<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/check"
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/unselected_image"
android:state_checked="false"/>
</selector>
如何使用颜色而不是可绘制对象?
我试过这个:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/bg_blue"/>
<item android:state_checked="false" android:color="@color/bg_light_grey"/>
</selector>
在名为bg_toggle.xml的颜色文件夹中定义了上述选择器
和
android:background="@color/bg_toggle"
这给出了一个例外:
android.view.InflateException: Binary XML file line #50: Error inflating class android.widget.ToggleButton
org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: tag requires a 'drawable' attribute or child tag defined a drawable
谢谢你