res下新建一个drawable目录,新建一个选择器.
toggle_selector.xml:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" ]]>
<item android:state_checked="true" android:drawable="@drawable/toggle_button_on"]]></item]]>
<item android:drawable="@drawable/toggle_button_off"]]></item]]>
</selector]]>
在使用控件时,布局上设置togglebutton的背景:
[html] view plaincopy
<ToggleButton
android:id="@+id/tb"
android:background="@drawable/toggle_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:checked="true"
/>
toggle_selector.xml:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" ]]>
<item android:state_checked="true" android:drawable="@drawable/toggle_button_on"]]></item]]>
<item android:drawable="@drawable/toggle_button_off"]]></item]]>
</selector]]>
在使用控件时,布局上设置togglebutton的背景:
[html] view plaincopy
<ToggleButton
android:id="@+id/tb"
android:background="@drawable/toggle_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:checked="true"
/>