2015-05-09 33 views
1

我想为我的文本视图分配一个text_color_selector。最初当我用android:textColor =“@ drawable/list_selector_nav_drawer_text”来做时,它工作正常(未压缩的文本颜色是黑色的)。但是当我使用下面的代码时,未压缩的文本颜色变为紫色(类似于HTML中访问过的链接的颜色)!我在做什么错:(?recyclerview的文本颜色选择器

我使用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> 

回答

2

上面的代码

setTextColor(R.drawable.list_selector_nav_drawer_text)

将转化为一个int an因此分配给内存中的一个随机数字,setTextColor会将其视为颜色而不是颜色状态列表。

您需要做的是将list_selector_nav_drawer_text xml选择器放置在您的color资源文件夹中,并从您的活动中调用上下文实例以获取状态列表。

样本:

//xml should be in the color resource folder  
tview.setTextColor(context.getResources().getColor(R.color.list_selector_nav_drawer_text)); 
+0

非常感谢。我也刚刚发现自己的解决方案,但并没有明白其中的道理。感谢您的解释。 – abdfahim

+0

谢谢,效果很好。 – Ali