通过编程设置后获得奇怪的字体大小 [英] Getting weird font size after setting it programmatically
问题描述
我正在尝试以编程方式设置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.
此外,sp
个单位.
Additionally, another SO thread discusses how to convert this to sp
units.
这篇关于通过编程设置后获得奇怪的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!