如何以编程方式为所有 TextViews 设置文本颜色? [英] How to set text color for all the TextViews programatically?
本文介绍了如何以编程方式为所有 TextViews 设置文本颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在动态创建 8 个 TextView
并将它们添加到我的布局中.我想设置他们的文字颜色.所以我在 color.xml 中声明了颜色代码,并将其设置为:
I am creating 8 TextView
s dynamically and adding them in my layout. I want to set their text color. So I have declared the color code in my color.xml and I am setting it as:
txt1.setTextColor(getResources().getColor(R.color.off_white));
但我必须单独为所有 TextView
重复执行此操作.有没有办法可以为所有 TextViews 全局设置它.类似于我们在 jQuery 中可以做的事情,例如:
But I have to redundantly do this for all the TextView
s individually. Is there a way wherein I can set it globally for all the TextViews. Something similar to what we can do in jQuery for example:
$('input[type="text"]').css('color','white');
推荐答案
您可以为此使用自定义 TextView
.
You can use Custom TextView
for that.
如图所示:
MyTextView mTxt = new MyTextView(getApplicationContext()); //Use MyTextView instead of TextView where you want to apply color
mTxt.setText("Some text");
您的自定义类 MyTextView
将类似于:
Your custom class MyTextView
would be something like:
public class MyTextView extends TextView{
public MyTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.setTextColor(Color.GREEN); //change color as per your need here.
}
}
希望有帮助.
这篇关于如何以编程方式为所有 TextViews 设置文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文