Monday, November 30, 2015

为什么TextView 设置了Selector 没有作用呢?

我真是很诧异,很多时候我们并没有深入的看文档,导致了这个问题,我也是醉了! 有空我要多看看文档!

我的设置:
selector:
error:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/icon_attendance_record"/>    <item android:drawable="@drawable/icon_attendance_record_press" android:state_checked="true"/>    <item android:drawable="@drawable/icon_attendance_record_press" android:state_pressed="true"/>    <item android:drawable="@drawable/icon_attendance_record_press" android:state_active="true"/>    <item android:drawable="@drawable/icon_attendance_record_press" android:state_focused="true"/>    <item android:drawable="@drawable/icon_attendance_record_press" android:state_checkable="true"/></selector>

right:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/icon_attendance_record_press" android:state_checked="true"/>    <item android:drawable="@drawable/icon_attendance_record_press" android:state_pressed="true"/>    <item android:drawable="@drawable/icon_attendance_record_press" android:state_active="true"/>    <item android:drawable="@drawable/icon_attendance_record_press" android:state_focused="true"/>    <item android:drawable="@drawable/icon_attendance_record_press" android:state_checkable="true"/>    <item android:drawable="@drawable/icon_attendance_record"/></selector>


you can see:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.

这里的每一个Item的改变,Android 会从第一个Item,寻找匹配当前的状态的Item.然后设置!

如果,讲Normal 放到第一个 就会一直匹配第一个!






No comments:

Post a Comment