android中设置TextView的颜色有方法setTextColor,这个方法被重载了,可以传入两种参数.
public void setTextColor(int color) {
mTextColor = ColorStateList.valueOf(color);
updateTextColors();
}
public void setTextColor(ColorStateList colors) {
if (colors == null) {
throw new NullPointerException();
}
mTextColor = colors;
updateTextColors();
}
下边就分别写出怎么使用这两个方法设置TextView的颜色:
TextView tv = new TextView(this);
tv.setText("Test set TextView's color.");
//方案一:代码中通过argb值的方式
tv.setTextColor(Color.rgb(255, 255, 255));
Resources resource = (Resources) getBaseContext().getResources();
ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
if (csl != null) {
tv.setTextColor(csl);
}
XmlResourceParser xrp = getResources().getXml(R.color.my_color);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
tv.setTextColor(csl);
} catch (Exception e) {
}
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/943.html
微信打赏
支付宝打赏
感谢您对作者mike的打赏,我们会更加努力! 如果您想成为作者,请点我