Change clickable TextView's color on focus and click?

后端 未结 8 1360
灰色年华
灰色年华 2020-11-28 05:26

I have a clickable TextView that I want to give some colors to. But I don\'t know how. Here are the relevant code snippets from my two files that I\'m working with:

相关标签:
8条回答
  • 2020-11-28 06:20

    Try this one.. It worked for me:

    File name: res/color/bg_tab_text_color.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="false" android:state_selected="false" android:color="@color/tab_unselected_text_color"/>
        <item android:state_pressed="true" android:color="@color/tab_selected_text_color"/>
        <item android:state_pressed="false" android:state_selected="true" android:color="@color/tab_selected_text_color"/>
        <item android:color="@color/tab_unselected_text_color"></item>
    </selector>
    

    Try setting the color in xml layout as:

    android:textColor="@color/bg_tab_text_color"
    
    0 讨论(0)
  • 2020-11-28 06:20

    Look in R.java class (it's generated automatically). You have something like that:

     public static final class color {
            public static final int gray_transparent=0x7f050001;
    }
    

    So in your code in line:

    title.setTextColor(R.color.textcolor);
    

    you're not setting values from textcolor.xml but int from R.java (which contains textcolor.xml address). The valid way to set color is:

    title.setTextColor(getResources().getColorStateList(R.color.textcolor));
    
    0 讨论(0)
提交回复
热议问题