我希望我的 Button 默认使用透明背景绘制,按下时使用深蓝色背景绘制,选择时使用浅蓝色背景绘制。我将按钮的背景设置为下面的选择器,并在Button.setSelected(!Button.isSelected())
单击按钮时调用。按下状态正常工作,但选择时 Button 以透明背景绘制。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/tranpsshape" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/tranpsshape" />
<item android:state_pressed="true" android:drawable="@drawable/pressedbuttonshape" />
<item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/selectedshape" />
<item android:state_enabled="true" android:drawable="@drawable/tranpsshape" />
<item android:state_focused="true" android:drawable="@drawable/tranpsshape" />
<item android:state_selected="true" android:drawable="@drawable/selectedshape" />
<item android:drawable="@drawable/tranpsshape" />
</selector>
选定形状.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#A3DAF2"/>
</shape>
按下按钮形状.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#4ABDE8/>
</shape>
转换形状.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#00000000"/>
</shape>