App*_*ide 5 android textcolor android-selector
我正在尝试应用通过代码创建的 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);
Run Code Online (Sandbox Code Playgroud)
我简单地将其应用到 TextView 以这种方式
myTextView.setTextColor(myList);
Run Code Online (Sandbox Code Playgroud)
并且不起作用。使用这个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>
Run Code Online (Sandbox Code Playgroud)
它可以在 xml 中设置文本颜色,也可以通过代码以这种方式设置
myTextView.setTextColor(myTextView.getContext().getResources().getColorStateList(R.drawable.textcolor_selector));
Run Code Online (Sandbox Code Playgroud)
我在网上搜索了解决方案,但我真的找不到导致此问题的原因,有人可以帮助我吗?
谢谢
也许您应该向状态列表添加默认值。在你的情况下,state_activated 的相反状态:
int[][] states = new int[][] { new int[] { android.R.attr.state_activated }, new int[] { -android.R.attr.state_activated } };
int[] colors = new int[] { Color.parseColor("#FFFF00"), Color.BLACK };
myList = new ColorStateList(states, colors);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5720 次 |
| 最近记录: |