TextView.setTextSize 行为异常 - 如何为不同屏幕动态设置 textview 的文本大小 [英] TextView.setTextSize behaves abnormally - How to set text size of textview dynamically for different screens

查看:21
本文介绍了TextView.setTextSize 行为异常 - 如何为不同屏幕动态设置 textview 的文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用 TextView.setTextSize() 工作异常.在调用 setTextSize 之后,如果我们得到一个 getTextSize,它会返回一个比我们之前设置的值高得多的值.

Calling TextView.setTextSize() is working abnormally. Right after the call to setTextSize if we get a getTextSize its returning a much higher value that what we set it to earlier.

这是我们正在做的事情:

Here's what we're doing:

zoomControl.setOnZoomInClickListener(new OnClickListener() {
    public void onClick(View view) {
        float size = mViewShabad.getTextSize() + 1;
        textView.setTextSize(size);
    }
});

有人见过吗?

推荐答案

这里的区别在于setTextSize(int size)方法中,单位类型默认为sp"或scaled"像素".对于每个屏幕密度(ldpi、mdpi、hdpi),该值将是不同的像素尺寸.

The difference here is that in the setTextSize(int size) method, the unit type by default is "sp" or "scaled pixels". This value will be a different pixel dimension for each screen density (ldpi, mdpi, hdpi).

getTextSize(),另一方面,返回文本的实际像素尺寸.

getTextSize(), on the other hand, returns the actual pixel dimensions of the text.

您可以使用 setTextSize(int unit, float size) 来指定单位类型.可以在 TypedValue 类中找到它的常量值,但其中一些是:

You can use setTextSize(int unit, float size) to specify a unit type. The constant values for this can be found in the TypedValue class, but some of them are:

TypedValue.COMPLEX_UNIT_PX   //Pixels

TypedValue.COMPLEX_UNIT_SP   //Scaled Pixels

TypedValue.COMPLEX_UNIT_DIP  //Device Independent Pixels

这篇关于TextView.setTextSize 行为异常 - 如何为不同屏幕动态设置 textview 的文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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