动态调整textview的字体大小

发布于 2024-12-20 23:22:24 字数 84 浏览 0 评论 0原文

是否可以根据屏幕分辨率动态调整文本视图中的字体大小?如果是的话怎么办?我正在从 mdpi avd 开发。但当应用程序安装在 hdpi 上时,文本显得太小。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2024-12-27 23:22:24

使用 textSize ,缩放后的像素 sp 单位是 alextsc是在暗示。

如果您确实想要动态并使字体尽可能大以填充宽度,那么可以使用 textWatcher 渲染文本并检查大小,然后动态调整字体。

以下内容相当具体,因为我在线性布局中有多个文本视图,并且只有当其中一个视图中的文本不适合时,才会将文本大小调整得较小。不过,它会给你一些可以使用的东西。

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");
    }
}
}

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

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");
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文