Currently, most color selector methods on the Internet are as follows:
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:color="#FFF" /> <item android:state_focused="true" android:color="#FFF" /> <item android:state_pressed="true" android:color="#FFF" /> <item android:color="#000" /></selector></span>
However, in some use cases, an error is reported, indicating that the drawable is not correct. After careful research, you can actually make the color into a drawable form before using it, in this way, you can directly use drawable selector. First, we define the color as drawable in the color file.
<span style="font-size:14px;"><drawable name="menu_bg_normal">#00000000</drawable><drawable name="menu_bg_pressed">#50000000</drawable></span>
Then define the drawable selector.
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/stu_menu_bg_pressed"/> <item android:drawable="@drawable/stu_menu_bg_normal"/></selector></span>
Bingo! So that you don't have to worry about the color selector problem !!
How to Use color selector to report errors