標籤:height 技術 http inf ccf androi 引用 medium family
什麼是開關?先看一下Switch的實現效果
和手機上開啟移動資料的按鈕很像
屬性:
- android:showText:設定on/off的時候是否顯示文字,boolean
- android:splitTrack:是否設定一個間隙,讓滑塊與底部圖片分隔,boolean
- android:switchMinWidth:設定開關的最小寬度
- android:switchPadding:設定滑塊內文字的間隔
- android:switchTextAppearance:設定開關的文字外觀,暫時沒發現有什麼用...
- android:textOff:按鈕沒有被選中時顯示的文字
- android:textOn:按鈕被選中時顯示的文字
- android:textStyle:文字風格,粗體,斜體寫劃線那些
- android:track:底部的圖片
- android:thumb:滑塊的圖片
- android:typeface:設定字型
使用這個控制項比較簡單,只要在布局檔案中引用,然後設定相關的屬性就可以了
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" tools:context="com.contentprovide.liuliu.togglebutton_test.MainActivity"> <Switch android:id="@+id/swh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:track="@drawable/tra" android:thumb="@drawable/a3" android:textOff="off" android:textOn="on" /></LinearLayout>
我這裡的track和thumb引用的兩個drawable資源檔是我自己定義的
tra.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/a2"></item><item android:state_checked="false" android:drawable="@drawable/a1"></item></selector>
a1.xml、a2.xml、a3.xml是自訂圖形,比較簡單,這裡就不記錄了
Android開關:Switch控制項