目前我正在使用此选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/green" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@color/green" /> <!-- focused -->
</selector>
我想让我为我的按钮设置默认的drawable,以防它没有按下而没有聚焦。我应该如何修改我的选择器呢?
答案 0 :(得分:0)
试试这个:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:drawable="@drawable/button"
android:state_focused="false"
android:state_pressed="false"
android:state_selected="false"/>
<!-- Pressed -->
<item android:drawable="@drawable/button_pressed"
android:state_focused="false"
android:state_pressed="true"
android:state_selected="false"/>
</selector>
答案 1 :(得分:0)
在 res / values / colors.xml 文件中为各种状态定义不同的颜色(请使用您自己的颜色的十六进制代码)
<color name="green_pressed">#ff00f000</color>
<color name="green_focused">#ff00f700</color>
<color name="green_default">#ff00ff00</color>
为不同的状态声明各种drawable
<强> button_focused_green.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/green_focused" />
<!-- optional, remove if you don't want round border -->
<corners android:radius="4dp" />
</shape>
<强> button_pressed_green.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/green_pressed" />
<!-- optional, remove if you don't want round border -->
<corners android:radius="4dp" />
</shape>
<强> button_default_green.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/green_default" />
<!-- optional, remove if you don't want round border -->
<corners android:radius="4dp" />
</shape>
在xml文件中声明StateListDrawable
,该文件将应用于Button
<强> button_green.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_focused_green" android:state_focused="true"/>
<item android:drawable="@drawable/button_pressed_green" android:state_pressed="true"/>
<item android:drawable="@drawable/button_default_green"/>
</selector>
在布局xml中设置Button
的背景属性
<Button
...
android:background="@drawable/button_green"
.../>
希望这有帮助。