Android 将TextView颜色设置为a<;选择器>;以编程方式

Android 将TextView颜色设置为a<;选择器>;以编程方式,android,android-resources,Android,Android Resources,我在res/color/redemptionlist\u item\u color.XML下的XML文件中定义了以下选择器: 以这种方式设置时,颜色不再改变。可以用这种方式将选择器分配给文本视图吗?我认为您可能需要添加findViewById或类似的内容 编辑:以上是不正确的,根据我的评论,正确的答案是 setTextColor(getResources().getColorStateList(R.color.redeemlist_item_color)); 拉斯曼是对的。您需要给TextV

我在res/color/redemptionlist\u item\u color.XML下的XML文件中定义了以下选择器:


以这种方式设置时,颜色不再改变。可以用这种方式将选择器分配给
文本视图吗?

我认为您可能需要添加
findViewById
或类似的内容


编辑:以上是不正确的,根据我的评论,正确的答案是

setTextColor(getResources().getColorStateList(R.color.redeemlist_item_color));

拉斯曼是对的。您需要给TextView一个ID,android:ID=“@+/something”。您可以使用该ID和findViewById检索对该特定对象的引用,然后可以设置文本颜色。

您可以尝试:

holder.label.setTextColor(getResources().getColor(R.color.receiveList\u item\u color))

而不是:

holder.label.setTextColor(R.color.receivelist\u item\u color)

您必须使用


我也在努力解决这个问题,如果你想使用一个
状态列表
,你需要在
color
resources文件夹中声明它,而不是
drawable
文件夹,并使用
setTextColor(getResources().getColorStateList(R.color.redemedislist\u item\u color))
确定,也许我遗漏了太多的代码。我已调用findViewById()。我的问题不是空指针异常或任何东西,视图加载正常。这是BaseAdapter子类中的代码。以下是我在HD_鼠标上的评论:好的,也许我遗漏了太多代码。我已调用findViewById()。我的问题不是空指针异常或任何东西,视图加载正常。这是BaseAdapter子类中的代码。让我重新表述一下:您需要将R.转换为一个值。我认为正确的函数是getViewbyid。我错了。。。试试这个:setTextColor(getResources().getColor(R.color.redemptionlist\u item\u color))@拉斯曼:这是不对的,应该使用setTextColor(getResources().getColorStateList(R.color.redemptionlist\u item\u color));但是GetResources()方法没有在CustomAdapter中获取listView@Vikky-听起来您需要将上下文传递给listview适配器,然后您可以执行以下操作:
android:textColor=“@drawable/selector\u listview\u text”
这不正确,应该使用getColorStateList方法而不是getColor。有关以编程方式设置ColorStateList的信息,请参阅。
Resources\getColorStateList(int)
在API级别23中被弃用。使用
Resources\getColorStateList(int,Theme)
ContextCompat.getColorStateList(Context,int)
holder.label.setTextColor(R.color.redeemlist_item_color);
setTextColor(getResources().getColorStateList(R.color.redeemlist_item_color));