我正在尝试将通过代码创建的 ColorStateList 应用为 TextView 的 TextColor。问题是,如果我使用在 xml 中定义的 ColorStateList,它可以工作,但是当我通过代码创建 ColorStateList 时不起作用。
这是我创建 ColorStateList 的方式
int[][] states = new int[][] { new int[] { android.R.attr.state_activated } };
int[] colors = new int[] { Color.parseColor("#FFFF00") };
myList = new ColorStateList(states, colors);
我以这种方式简单地将它应用于 TextView
myTextView.setTextColor(myList);
并且不起作用。使用这个 xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:color="@color/yellow" />
<item android:color="@color/black" />
</selector>
它可以通过这种方式在xml中设置文本颜色并通过代码
myTextView.setTextColor(myTextView.getContext().getResources().getColorStateList(R.drawable.textcolor_selector));
我已经在网上搜索了一个解决方案,但我真的找不到导致这个问题的原因,有人可以帮助我吗?
谢谢你