介紹
Switch是在4.0以后推出的猜惋,所以要注意開發(fā)時(shí)的minsdk設(shè)置,google在API 21后也推出support v7 包下的SwitchCompa的Material Design
開關(guān)控件谦炬,對低版本的有了更好的的支持咱台。其實(shí)switch的應(yīng)用場景和ToggleButton類似肄程,多應(yīng)用于兩種狀態(tài)的切換。
<Switch
android:id="@+id/is_receive_message"
android:layout_width="@dimen/x50"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/y10"
android:background="@android:color/transparent"
android:checked="false"
android:textOff=""
android:textOn=""
android:thumb="@drawable/thumb"
android:track="@drawable/track" />
屬性介紹
android:typeface="normal":設(shè)置字體類型
android:track="":設(shè)置開關(guān)的軌跡圖片
android:textOff="開":設(shè)置開關(guān)checked的文字
android:textOn="關(guān)":設(shè)置開關(guān)關(guān)閉時(shí)的文字
android:thumb="":設(shè)置開關(guān)的圖片
android:switchMinWidth="":開關(guān)最小寬度
android:switchPadding="":設(shè)置開關(guān) 與文字的空白距離
android:switchTextAppearance="":設(shè)置文本的風(fēng)格
android:checked="":設(shè)置初始選中狀態(tài)
android:splitTrack="true":是否設(shè)置一個(gè)間隙详幽,讓滑塊與底部圖片分隔(API 21及以上)
android:showText="true":設(shè)置是否顯示開關(guān)上的文字(API 21及以上
在代碼中使用
btn_notice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
Android UI(Switch)詳解 :
http://blog.csdn.net/qq_28057577/article/details/52261641