1. Switch类
Switch
类被用来展示状态。
android:checked
设置是否打开
自定义Switch
,
android:track
设置背景android:thumb
设置选择背景android:switchMinWidth
设置宽度
背景文件switch_track.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:shape="rectangle">
<size android:height="30dp" />
<corners android:radius="15dp"/>
<solid android:color="#ff8b8b8b" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<size android:height="30dp" />
<corners android:radius="15dp"/>
<solid android:color="#ffff8c00" />
</shape>
</item>
</selector>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
选择背景文件switch_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:shape="rectangle">
<size android:height="30dp" android:width="30dp" />
<corners android:radius="15dp"/>
<solid android:color="#ffffffff" />
<stroke android:width="1dp" android:color="#ff8b8b8b" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<size android:height="30dp" android:width="30dp" />
<corners android:radius="15dp"/>
<solid android:color="#ffffffff" />
<stroke android:width="1dp" android:color="#ffff8c00" />
</shape>
</item>
</selector>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Switch
引用自定义样式
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="20dp"
android:track="@drawable/switch_track"
android:thumb="@drawable/switch_thumb"
android:checked="true"/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
效果如下
2. ToggleButton类
android:textOn
设置选中时文本android:textOff
设置取消选中时文本
配置文件
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="关闭"
android:textOn="打开"
android:checked="true" />
- 1
- 2
- 3
- 4
- 5
- 6
效果如下
参考资料:http://blog.youkuaiyun.com/qq_34763699/article/details/54954394
相关文章
Android RadioButton和CheckBox控件
Android Switch和ToggleButton控件