为什么我不能在Android中以编程方式设置我的文本大小?

为什么我不能在Android中以编程方式设置我的文本大小?

问题描述:

您好。我想以编程方式设置我的文本大小所以在第一步中我在我的dimen Xml文件中添加了dimen标签,如下所示:



hello. i wanted to set my text size programmatically so in first step i added "dimen" tag in my dimen Xml file like this :

<dimen name="txtsize">20sp</dimen> 



之后我写了3行代码:


after that i wrote 3 lines of code :

tv=new TextView(this);
tv.setTextSize(R.dimen.txtsize);
tv.setText("hi guys !")



但不幸的是文字消失了! 请注意如果我将我的代码更改为此


but unfortunately the text was disappeared ! please notice that if i change my code to this

tv.setTextSize(20);



它运作良好!但我不想直接设置文本大小。

请与我分享您的有用想法。谢谢......


it works well ! but i dont want to set the text size directly .
please share your useful ideas with me . thanks ...

试试这个:

try this:
tv.setTextSize(getResources().getDimension(R.dimen.txtsize));



参考: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension [ ^ ]