基础使用
xml布局中
<Switch
android:id="@+id/sc_main_xuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
java代码中
Switch viewById = (Switch) findViewById(R.id.sc_main_xuan);
mViewById1 = (Switch) findViewById(R.id.open);
viewById.setChecked(true);
mViewById1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// TODO Auto-generated method stub
if (b) {
Toast.makeText(Main3ActivitySwitch.this, "打开蓝牙", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Main3ActivitySwitch.this, "关闭蓝牙", Toast.LENGTH_SHORT).show();
}
}
});
常用的属性
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及以上)
自定义
https://www.jianshu.com/p/4e436300f328
用到了 android:thumb="":设置开关的图片
和android:track="":设置开关的轨迹图片
xml
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/switch_custom_thumb_selector"
android:track="@drawable/switch_custom_track_selector" />
开关选择器
switch_custom_thumb_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_custom_thumb_on" android:state_checked="true" />
<item android:drawable="@drawable/switch_custom_thumb_off" android:state_checked="false" />
</selector>
选择的图片shape选中
switch_custom_thumb_on
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#94C5FF" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
不选中
switch_custom_thumb_off
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#AAA" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
switch_custom_thumb_selector就做完了:设置开关的图片
轨迹图片switch_custom_track_selector选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_custom_track_on" android:state_checked="true" />
<item android:drawable="@drawable/switch_custom_track_off" android:state_checked="false" />
</selector>
选中switch_custom_track_on
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#B6D6FE" />
<stroke
android:width="5dp"
android:color="#00000000" />
<corners android:radius="20dp" />
</shape>
switch_custom_track_off不选中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#E3E3E3" />
<stroke
android:width="5dp"
android:color="#00000000" />
<corners android:radius="20dp" />
</shape>
加上文字的Switch
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textOff="OFF"
android:textOn="ON"
android:switchTextAppearance="@style/SwitchTheme"
android:thumb="@drawable/switch_custom_thumb_selector"
android:track="@drawable/switch_custom_track_selector" />
就是添加了 android:switchTextAppearance="@style/SwitchTheme"
让字体变颜色
<style name="SwitchTheme" parent="@android:style/TextAppearance.Small">
<item name="android:textColor">@drawable/switch_text_selector</item>
</style>
switch_text_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFF" android:state_checked="false" />
<item android:color="#000" android:state_checked="true" />
</selector>
效果图