android中设置TextView的颜色有方法setTextColor,这个方法被重载了,可以传入两种参数
下边就分别写出怎么使用这两个方法设置TextView的颜色:
这种方法也就是传入int color值,这个int不是R文件中自动分配的int值,所以要注意。这是Color类中的静态方法构造出来的颜色int值。
这种方法是通过ColorStateList得到xml中的配置的颜色的。好多需要xml中配置的都要类似这样的映射xml文件。
还有种方法Java代码:
- XmlResourceParser xrp = getResources().getXml(R.color.my_color);
- try {
- ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
- tv.setTextColor(csl);
- } catch (Exception e) {
- }