如何在运行时以编程方式使用 xml 中的维度设置文本大小? [英] How to set text size using dimension from xml at runtime programmatically?
本文介绍了如何在运行时以编程方式使用 xml 中的维度设置文本大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在dimens.xml中,我有:
In dimens.xml, I have:
<dimen name="text_medium">18sp</dimen>
在运行时,我得到这个值并设置文本视图的文本大小:
In runtime, I get this value and set the text size of a text view:
int size = context.getResources().getDimensionPixelSize(R.dimen.text_medium);
textView.setTextSize(size).
在 10 英寸平板电脑 (1280 x 800) 上,一切正常;但在手机 (800 x 480) 上,文本视图的字体非常大.在平板电脑上,大小等于 18;电话里是 27 岁.
On a 10″ tablet (1280 x 800), everything is ok; but on a phone (800 x 480), the text view has a very large font. On the tablet, the size equals 18; on the phone, it's 27.
如果我手动设置大小:
textView.setTextSize(size)
两个设备上的大小都是正常的.
the size is normal on both devices.
推荐答案
在dimens.xml中添加维度:
<dimen name="text_medium">18sp</dimen>
在代码中设置大小:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.text_medium));
这篇关于如何在运行时以编程方式使用 xml 中的维度设置文本大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文