Setting Android TextView text color programmatical

2019-04-29 21:52发布

问题:

I have a listview of simple text items. The TextView in those items looks like this (it's wrapped inside a RelativeLayout):

<TextView
  android:id="@+id/text_language_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_centerVertical="true"
  android:duplicateParentState="true"
  android:textSize="16sp"
  android:textStyle="bold" />

I want to use the following color state selector for the text, which I have called "dark_list_text_states.xml":

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@android:color/white"/>
    <item android:color="@color/dark_list_text"/>

</selector>

Normally, of course, I could just set that to the android:textColor property in the xml, but in this case, I need to set one particular item programmatically to a different color, using this selector ("blue_text_states.xml"):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@android:color/white"/>
    <item android:color="@color/selected_blue"/>

</selector>

So I set the text color in the Adapter (language_name and selected are defined earlier in the code):

        TextView text_language_name = (TextView)view.findViewById(R.id.text_language_name);
        if (text_language_name != null) {
            text_language_name.setText(language_name);
            int text_color = selected
                           ? getResources().getColor(R.color.blue_text_states)
                           : getResources().getColor(R.color.dark_list_text_states);
            text_language_name.setTextColor(text_color);
            text_language_name.setDuplicateParentStateEnabled(true);
        }

And this works fine, except for one problem: the text color doesn't change when the item is clicked. Even though I call setDuplicateParentStateEnabled(true) on the item right after setTextColor(), that setting is completely ignored.

I have tested this by commenting out the Java code that sets the text color and just setting it in the xml, and the selector works just fine. But if I set textColor programmatically, it is completely incapable of duplicating the parent state.

Has anyone seen this before? Is there a workaround, or is this just an Android bug I have to live with?

回答1:

OK, found my mistake. I should be using getResources().getColorStateList() instead of getResources().getColor(). Leaving this here for anyone else who makes the same mistake.



回答2:

From colors.xml: textview.setTextColor(getResources().getColor(R.color.your_color));

From Android system: textview.setTextColor(Color.BLACK);

Enjoy :)