效果图 布局Switch是在4.0以后推出的,要注意开发时的minsdk设置,google在API 21后也推出support v7 包下的SwitchCompa的Material Design
- @style
- 代码设置轨道和按钮
switchCompat.setTrackResource(R.mipmap.ic_launcher);//轨道 自定义图片
switchCompat.setThumbResource(R.mipmap.ic_launcher);//按钮 自定义图片
- 点击的处理
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
Log.i("TAG","开的点击处理--------");
}else {
Log.i("TAG","关的点击处理--------");
}
}
});
属性
- android:typeface=“normal”:设置字体类型
- android:track=“”:设置开关的轨迹图片
- android:textOff=“开”:设置开关checked的文字
- android:textOn=“关”:设置开关关闭时的文字
- android:thumb=“”:设置开关的图片
- android:switchMinWidth=“”:开关最小宽度
- android:switchPadding=“”:设置开关 与文字的空白距离
- android:switchTextAppearance=“”:设置文本的风格
- android:checked=“”:设置初始选中状态
- android:splitTrack=“true”:是否设置一个间隙,让滑块与底部图片分隔(API 21及以上)
android:showText=“true”:设置是否显示开关上的文字(API 21及以上)



