动态调整的TextView的字体 [英] Dynamically resize font of textview

查看:287
本文介绍了动态调整的TextView的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能根据屏幕分辨率动态地重新大小的字体在一个TextView?如果是的话怎么样?我是来自于mdpi AVD发展。但是,当应用程序被安装在华电国际文本显示太小了。

Is it possible to dynamically re-size fonts in a textview according to screen resolution? If yes how? I'm developing from an mdpi avd. But when the app is installed on hdpi text appears too small.

推荐答案

使用的 TEXTSIZE 和缩放像素个SP单元是alextsc的暗示。

Use textSize and the scaled pixels sp unit is what alextsc is implying.

如果你真的想成为对动态,让你字体尽可能大,填补了宽度则有可能与textWatcher呈现文本,并检查尺寸,然后调整上飞的字体。

If you realy want to be dynmic and make you font as big as possible to fill up the width then it is possible with a textWatcher to render the text and check the size and then tweak the fonts on the fly.

以下是相当具体,因为我有一个线性布局多个文本的意见和这只是调整大小文本一次小文在其中一人将不适合。它会给你带的东西,虽然工作。

The following is rather specific as I have multiple text views in a linear layout and this only resizes the text smaller once the text in one of them will not fit. It will give you something to work with though.

class LineTextWatcher implements TextWatcher {

static final String 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 start, 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.toString());
            if ((int) size > mTV.getWidth()) {
                float ts = mTV.getTextSize();
                Log.w(TAG, "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(TAG, "afterTextChanged Child not textView");
                    }
                }
            }
        }
    } else {
        Log.v(TAG, "afterTextChanged parent not LinearLayout");
    }
}
}

这篇关于动态调整的TextView的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆