【问题标题】:Text color selector for recyclerviewrecyclerview 的文本颜色选择器
【发布时间】:2015-05-09 01:19:39
【问题描述】:

我想为我的文本视图分配一个 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>

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    以上代码

    setTextColor(R.drawable.list_selector_nav_drawer_text)

    将转换为 int,因此转换为内存中分配的随机数,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));
    

    【讨论】:

    • 非常感谢..我也刚刚找到他的解决方案,但不明白原因。感谢您的解释。
    猜你喜欢
    • 2012-12-14
    • 1970-01-01
    • 2014-12-17
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 2015-05-27
    相关资源
    最近更新 更多