由网友(先森,别耍帅)分享简介:我有每排一个TextView一个ListView。我有在row.xml设置默认color.xml 我有不同的状态不同颜色 <选择的xmlns:机器人=http://schemas.android.com/apk/res/android> <! - pressed - > &L...![Android编程课程1TextView国际化](https://p.xsw88.cn/allimgs/daicuo/20230907/5352.png)
我有每排一个TextView一个ListView。我有在row.xml设置默认color.xml
我有不同的状态不同颜色
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android> <! - pressed - > <项目 机器人:颜色=#FFFFFF 机器人:STATE_ pressed =真/> <! - 集中 - > <项目的android:state_selected =真 机器人:颜色=#8b8989/> <! - 默认 - > <项目的android:颜色=#FFFFFF/>< /选择>
这就像一个魅力。但是,当我试着去改变为code某些行的颜色,这似乎并没有工作。该second_color.xml看起来一样的,但不同的颜色。颜色发生变化,但对于其他国家(不是默认的)没有什么变化。
我改变颜色是这样的:
TextView的TL =(TextView中)v.findViewById(R.id.textlabel);tl.setTextColor(。的getContext()getResources()的getColor(R.color.second_color));
解决方案
解决它!
![Android编程课程1TextView国际化](https://p.xsw88.cn/allimgs/daicuo/20230907/5352.png)
在订单C它需要创建一个ColorStateList设置这$ C $。
ColorStateList CL = NULL; 尝试{ 。XmlResourceParser XRP = getResources()的getXML(R.color.live_color); CL = ColorStateList.createFromXml(getResources(),XRP); }赶上(例外前){} 如果(CL!= NULL){ tl.setTextColor(CL); }
I have a ListView with a TextView in each row. I have a default color.xml with is set in the row.xml
I have different colors for different states
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- pressed -->
<item
android:color="#ffffff"
android:state_pressed="true"/>
<!-- focused -->
<item android:state_selected="true"
android:color="#8b8989"/>
<!-- default -->
<item android:color="#ffffff"/>
</selector>
This works like a charm. But when Im trying to change the color for some rows in code, this doesn't seem to work. The second_color.xml looks just the same, but with different colors. The color is changed, but for the other states (not default) nothing changes.
I change the color like this:
TextView tl = (TextView) v.findViewById(R.id.textlabel);
tl.setTextColor(getContext().getResources().getColor(R.color.second_color));
解决方案
Solved it!
In order to set this in code it's required to create a ColorStateList.
ColorStateList cl = null;
try {
XmlResourceParser xrp = getResources().getXml(R.color.live_color);
cl = ColorStateList.createFromXml(getResources(), xrp);
} catch (Exception ex) {}
if(cl != null){
tl.setTextColor(cl);
}
相关推荐
最新文章