将 TextView 颜色设置为以编程方式
我在 res/color/redeemlist_item_color.xml 下的 XML 文件中定义了以下选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#FFFFFF" /> <!-- pressed -->
<item android:state_selected="true"
android:color="#FFFFFF" /> <!-- focused -->
<item android:color="#000000" /> <!-- default -->
</selector>
我在 ListView
项布局中还有一个 TextView
。当我将此 TextView
上的 android:textColor
设置为 XML 中的上述选择器时,选择该项目时颜色会正确更改。但是,我尝试通过以下方式以编程方式设置此资源:
holder.label.setTextColor(R.color.redeemlist_item_color);
以这种方式设置时,颜色不再更改。可以通过这种方式将选择器分配给 TextView
吗?
I have the following selector defined in an XML file under res/color/redeemlist_item_color.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#FFFFFF" /> <!-- pressed -->
<item android:state_selected="true"
android:color="#FFFFFF" /> <!-- focused -->
<item android:color="#000000" /> <!-- default -->
</selector>
I also have a TextView
in a ListView
item layout. When I set android:textColor
on this TextView
to the above selector in XML, then the color changes correctly when the item is selected. However, I am trying to set this resource programmatically in the following way:
holder.label.setTextColor(R.color.redeemlist_item_color);
When set in this way, the color no longer changes. Can a selector be assigned to a TextView
in this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您可能需要添加
findViewById
或类似的内容编辑:根据我的评论,上述内容是不正确的,正确的答案是
I think you might need to add
findViewById
or something of that varietyEdit: the above is incorrect as per my comment the proper answer is
你必须使用 getColorStateList()
我也在努力这个问题,如果你想使用
state list
,你需要在color
资源文件夹中声明它,而不是在drawable
文件夹中,并使用setTextColor(getResources().getColorStateList(R.color.redeemlist_item_color))
。You have to use getColorStateList()
I was also struggling with this problem, if you want to have use a
state list
, you need to declare it in thecolor
resources folder, instead of thedrawable
folder, and use thesetTextColor(getResources().getColorStateList(R.color.redeemlist_item_color))
.您可以尝试:
holder.label.setTextColor(getResources().getColor(R.color.redeemlist_item_color));
而不是:
holder.label.setTextColor(R.color.redeemlist_item_color);
You can try:
holder.label.setTextColor(getResources().getColor(R.color.redeemlist_item_color));
instead of :
holder.label.setTextColor(R.color.redeemlist_item_color);
示例
我更喜欢
ContextCompat.getColorStateList
因为它适用于所有 APIExample
I prefer
ContextCompat.getColorStateList
as it work for all API拉斯曼是正确的。您需要给 TextView 一个 ID,android:id="@+/something"。您可以使用该 ID 和 findViewById 检索对该特定内容的引用,然后可以设置文本颜色。
Rasman is correct. You need to give the TextView an ID, android:id="@+/something". You retrieve a reference to that particular using that ID and findViewById, and then you may set the text color.