1、switchbutton是項目開發中常用的開關控制項,可直接引用系統中的switchbutton,剛剛寫了一個小demo,研究了一下switchbutton中的一些常用屬性:
swThumbDrawable: drawable of thumb(按鈕的映像) swThumb_margin: set inner margin between thumb and edges swThumb_marginLeft/Top/Bottom/Right: set margin for specific edge swThumb_width: set the width of thumb, probably used for gradient drawable(按鈕的寬) swThumb_height: set the height of thumb(按鈕的高) swThumbColor: like the onColor(按鈕的顏色,預設為灰色,開啟時根據類型所定) swMeasureFactor: factor limit the minimum width equals almost (the height of thumb * measureFactor)(拇指高度) swTextOn:按鈕開始時顯示的內容 swTextOff:按鈕關閉的時顯示的內容 swBackColor:開關背景色 swBackDrawable:開關背景圖片 2、自己寫的小demo:
<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:orientation="vertical" tools:context="com.example.wls.myswitchbutton.MainActivity"> <com.alanapi.switchbutton.SwitchButton android:id="@+id/switchButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" /> <com.alanapi.switchbutton.SwitchButton android:id="@+id/switchButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" app:swThumbHeight="30dp" app:swThumbWidth="30dp" /> <com.alanapi.switchbutton.SwitchButton android:id="@+id/switchButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" app:swThumbHeight="30dp" app:swThumbWidth="30dp" app:swThumbColor="#fff" app:swBackColor="@color/radiocolor"/> <com.alanapi.switchbutton.SwitchButton android:id="@+id/switchButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" app:swThumbHeight="30dp" app:swThumbWidth="30dp" app:swTextOn="ON" app:swTextOff="OFF" style="@style/SwitchButtonDefault"/> <com.alanapi.switchbutton.SwitchButton android:id="@+id/switchButton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" app:swThumbHeight="30dp" app:swThumbWidth="30dp" app:swTextOn="ON" app:swTextOff="OFF" style="@style/SwitchButtonFlyme"/> <com.alanapi.switchbutton.SwitchButton android:id="@+id/switchButton6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" app:swThumbHeight="30dp" app:swThumbWidth="30dp" app:swTextOn="ON" app:swTextOff="OFF" style="@style/SwitchButtonIOS"/> <com.alanapi.switchbutton.SwitchButton android:id="@+id/switchButton7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" app:swThumbHeight="30dp" app:swThumbWidth="30dp" app:swTextOn="ON" app:swTextOff="OFF" style="@style/SwitchButtonMD"/> <com.alanapi.switchbutton.SwitchButton android:id="@+id/switchButton8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" app:swThumbHeight="30dp" app:swThumbWidth="30dp" app:swTextOn="ON" app:swTextOff="OFF" style="@style/SwitchButtonMiui"/></LinearLayout>
上面一系列SwitButton所對應的形式如圖所示:
其中第三個按鈕中XML中的Color如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#0757ec" android:state_checked="true"/> <item android:color="#AAAAAA" android:state_checked="false"/>
這裡供上源碼地址: SwitchButton