recyclerview 的文本颜色选择器
Text color selector for recyclerview
我想为我的文本视图分配一个 text_color_selector。最初当我使用 android:textColor="@drawable/list_selector_nav_drawer_text" 时,它工作正常(未按下的文本颜色为黑色)。但是当我使用下面的代码时,未按下的文本颜色变成紫色(类似于 HTML 中访问过的链接的颜色)!我做错了什么:(?
我正在使用 recyclerview。
public void removeNavItemSelected(View v){
if(v!= null) {
TextView tview;
tview = (TextView) v.findViewById(R.id.title);
tview.setTextColor(R.drawable.list_selector_nav_drawer_text); // Why on this earth color becomes purple rather than black !!!
}
}
list_selector_nav_drawer_text
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="@color/blue" >
</item>
<item android:color="@color/black" >
</item>
</selector>
以上代码
setTextColor(R.drawable.list_selector_nav_drawer_text)
将转换为一个 int,因此转换为内存中分配给它的随机数,setTextColor
会将其视为颜色而不是颜色状态列表。
你需要做的是将 list_selector_nav_drawer_text
xml 选择器放在你的 color
资源文件夹中,并从你的 activity 调用上下文实例来获取状态列表.
样本:
//xml should be in the color resource folder
tview.setTextColor(context.getResources().getColor(R.color.list_selector_nav_drawer_text));
我想为我的文本视图分配一个 text_color_selector。最初当我使用 android:textColor="@drawable/list_selector_nav_drawer_text" 时,它工作正常(未按下的文本颜色为黑色)。但是当我使用下面的代码时,未按下的文本颜色变成紫色(类似于 HTML 中访问过的链接的颜色)!我做错了什么:(?
我正在使用 recyclerview。
public void removeNavItemSelected(View v){
if(v!= null) {
TextView tview;
tview = (TextView) v.findViewById(R.id.title);
tview.setTextColor(R.drawable.list_selector_nav_drawer_text); // Why on this earth color becomes purple rather than black !!!
}
}
list_selector_nav_drawer_text
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="@color/blue" >
</item>
<item android:color="@color/black" >
</item>
</selector>
以上代码
setTextColor(R.drawable.list_selector_nav_drawer_text)
将转换为一个 int,因此转换为内存中分配给它的随机数,setTextColor
会将其视为颜色而不是颜色状态列表。
你需要做的是将 list_selector_nav_drawer_text
xml 选择器放在你的 color
资源文件夹中,并从你的 activity 调用上下文实例来获取状态列表.
样本:
//xml should be in the color resource folder
tview.setTextColor(context.getResources().getColor(R.color.list_selector_nav_drawer_text));