Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 动态调整textview的字体大小大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以根据屏幕分辨率动态调整文本视图中的字体大小?如果有,怎么样?我正在开发@L_497_0@mdpi avd.但是当应用程序安装在hdpi文本时显得太小.

解决方法

使用 textSize和缩放像素sp单位是alextsc所暗示的.

如果你真的想成为dynmic并使你的字体尽可能大以填充宽度,那么可以使用textWatcher渲染文本并检查大小,然后动态调整字体.

以下内容非常具体,因为我在线性布局中有多个文本视图,只有当其中一个文本中的文本不适合时,才会调整文本的大小.它会给你一些工作.

class LineTextWatcher implements TextWatcher {

static final @R_772_10495@ng TAG = "IpBike";
TextView mTV;
Paint mPaint;

public LineTextWatcher(TextView text) {
    mTV = text;
    mPaint = new Paint();
}

public void beforeTextChanged(CharSequence s,int start,int count,int after) {
}

public void ontextChanged(CharSequence s,int before,int count) {
}

public void afterTextChanged(Editable s) {
    // do the work here.
    // we are looking for the text not fitTing.
    ViewParent vp = mTV.getParent();
    if ((vp != null) && (vp instanceof LinearLayout)) {
        LinearLayout parent = (LinearLayout) vp;
        if (parent.getVisibility() == View.VISIBLE) {
            mPaint.setTextSize(mTV.getTextSize());
            final float size = mPaint.measureText(s.to@R_772_10495@ng());
            if ((int) size > mTV.getWidth()) {
                float ts = mTV.getTextSize();
                Log.w@R_801_10374@,"Text ellipsized TextSize was: " + ts);
                for (int i = 0; i < parent.getChildCount(); i++) {
                    View child = parent.getChildAt(i);
                    if ((child != null) && (child instanceof TextView)) {
                        TextView tv = (TextView) child;
                        // first off we want to keep the verticle
                        // height.
                        tv.setHeight(tv.getHeight()); // freeze the
                                                      // height.

                        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,tv.getTextSize() - 1);
                    } else {
                        Log.v@R_801_10374@,"afterTextChanged Child not textView");
                    }
                }
            }
        }
    } else {
        Log.v@R_801_10374@,"afterTextChanged parent not LinearLayout");
    }
}
}

大佬总结

以上是大佬教程为你收集整理的android – 动态调整textview的字体大小全部内容,希望文章能够帮你解决android – 动态调整textview的字体大小所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。