Set the color selector selector for TextView

I encountered a situation in the project. If a TextView can be clicked-the text color is black, when it is clicked, it becomes unclickable-the text color is blue.


Then of course the selector is selected,

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/black_333333" android:state_enabled="true" />
    <item android:color="@color/blue" android:state_enabled="false" />
</selector>

Note that the color setting in the code is not drawable!

Note!  The attribute of android:color cannot be associated (my Studio 2.2.2, cannot be associated)

Knock it out directly! Knock it out directly! Knock it out directly!

<TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

             android:textColor="@drawable/selector_text_color" />

Then set it directly to the android:textColor attribute.


Follow-up:

I found out why the android:color attribute can’t be associated . The above Selector was created directly in the drawavble folder.

When setting the item, the android:color attribute cannot be associated .

You can right-click directly on the res folder -> new -> Android resources directory;

 Create the color folder and name it color


Then you can directly set the android:color attribute by creating a new selector file under the color file .


Guess you like

Origin blog.csdn.net/u011288271/article/details/79760342