为什么我不能在Android中以编程方式设置我的文本大小? [英] Why I Cant Set My Text Size Programmatically In Android ?

查看:98
本文介绍了为什么我不能在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 [ ^ ]


这篇关于为什么我不能在Android中以编程方式设置我的文本大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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