在drawable文件夹下创建selector_toggle.xml文件
代码如下:
<?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/switch_in_show" /> <!--选中状态,显示的图片switch_in_show-->
<item android:drawable="@drawable/switch_in_hide" /> <!--未选中状态,显示的图片switch_in_hide-->
</selector>
在页面xml文件中,调用如下:
<ToggleButton
android:id="@+id/shop_product_isrecordstock"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/selector_toggle"
android:text=""
android:textOff=""
android:textOn="" />