天天看點

android用戶端學習-開關按鈕

在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="" />