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:开关背景图片
<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