2
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/key" />
<item
android:state_pressed="true"
android:drawable="@drawable/key_pressed" />
<item
android:state_checkable="true"
android:drawable="@drawable/key_function" />
<item
android:state_checkable="true"
android:state_pressed="true"
android:drawable="@drawable/key_pressed" />
<item
android:state_checkable="true"
android:state_checked="true"
android:drawable="@drawable/key_function" />
<item
android:state_checkable="true"
android:state_checked="true"
android:state_pressed="true"
android:drawable="@drawable/key_pressed" />
</selector>

测试结果:

普通键->@drawable/key

普通按键 ->@drawable/key_pressed

粘滞键->@drawable/key_function

按下粘滞键 ->@drawable/key_function <---这是问题所在

4

1 回答 1

1

选择不是基于“最佳匹配”,而只是满足该州最低标准的第一个项目。

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

所以这是错误的可绘制对象,因为其中一个状态与当前状态匹配

<item
android:state_checkable="true"
android:drawable="@drawable/key_function" />  
<item
android:state_checkable="true"
android:state_checked="true"
android:drawable="@drawable/key_function" />

试着放

<item
android:state_checkable="true"
android:state_checked="true"
android:state_pressed="true"
android:drawable="@drawable/key_pressed" />

在顶部或将条件添加到以前的案例。

于 2012-09-09T19:52:44.947 回答