按照此方法,使您ToogleButton
具有背景色红时,绿灯亮时OFF
第一个,在可绘制文件夹中创建tooglebutton_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/togglebutton_on"
android:state_checked="true" />
<item android:drawable="@drawable/togglebutton_off"
android:state_checked="false"
/>
</selector>
二,创建togglebutton_on.xml
在绘制文件夹
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#ff0000" /> // red color
</shape>
三,在绘制文件夹中创建togglebutton_off.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#00FF00" /> // green color
</shape>
最后,在你切换按钮
<ToggleButton
android:id="@+id/btnMon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tooglebutton_selector" //set background of ToggleButton to tooglebutton_selector
/>
向我们展示您的尝试。一般来说,您需要使用[颜色状态列表](http://developer.android.com/guide/topics/resources/color-list-resource.html)或[可绘制状态列表](http:// developer。 android.com/guide/topics/resources/drawable-resource.html#StateList)来完成你所描述的内容。 –