通过编程设置后获得奇怪的字体大小 [英] Getting weird font size after setting it programmatically

查看:168
本文介绍了通过编程设置后获得奇怪的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式设置TextView字体大小.

I am trying to set TextView font size programmatically.

txtView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

但是当我通过

Toast.makeText(this, "txt: " + txtView.getTextSize(), Toast.LENGTH_SHORT).show();

Toast显示大小为"24",比我之前设置的大小大50%.

The Toast shows that the size is "24" which is 50% larger then the one I set before.

我测试了多个值,并且总是得到相同的结果:

I tested multiple values and always get the same result:

8变成12
10变成15
15变成22.5

8 becomes 12
10 becomes 15
15 becomes 22.5
etc.

我尝试设置的字体总是变大50%.

The font I am trying to set always becomes 50% larger.

为什么会这样?我在哪里犯错了?

Why is this happening? Where am I making a mistake?

推荐答案

我相信这是因为

I believe this is because TextView.getTextSize() returns the text size in pixels, not sp units, regardless of what unit type you used to set the text size in the first place.

此外,另一个SO线程讨论了如何转换到sp个单位.

Additionally, another SO thread discusses how to convert this to sp units.

这篇关于通过编程设置后获得奇怪的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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