今天准备为安卓应用中的Button设置样式,在可用(enabled)的时候是深灰色,在不可用(disabled)的时候是浅灰色。
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_shape_normal"
android:state_pressed="false"/>
<item android:drawable="@drawable/button_shape_pressed"
android:state_pressed="true"/>
<item android:drawable="@drawable/button_shape_normal"
android:state_enabled="true"/>
<item android:drawable="@drawable/button_shape_disabled"
android:state_enabled="false"/>
</selector>
用的是android:state_enabled 这个属性。界面中的两个Button,初始设置就是不可用的,也就是说,他们应该浅灰色的。
但实际运行发现所有Button都是深灰色的,也就是说,我这个state_enabled 的设置没有起作用。这是为什么呢?
看看Xml文件,怎么看都看不出哪里有问题。
这个selector中,除了state_enabled这个状态之外,上面还有state_pressed这个状态,并且state_pressed是起作用的。
于是,根据一丝可以称之为“经验”的感觉,我试着把state_enabled和state_pressed换了一下位置,也就是把state_enabled放在最前面:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_shape_normal"
android:state_enabled="true"/>
<item android:drawable="@drawable/button_shape_disabled"
android:state_enabled="false"/>
<item android:drawable="@drawable/button_shape_normal"
android:state_pressed="false"/>
<item android:drawable="@drawable/button_shape_pressed"
android:state_pressed="true"/>
</selector>
那么是否可以得出这样的结论,在selector中的这些状态,是有优先级之分的吗?