Switch控件详解

原生效果

5.x

4.x

布局

<Switch
android:id="@+id/setting_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

属性

Attribute Name Related Method Description
android:showText setShowText(boolean) Whether to draw on/off text.
android:splitTrack setSplitTrack(boolean) Whether to split the track and leave a gap for the thumb drawable.
android:switchMinWidth setSwitchMinWidth(int) Minimum width for the switch component Must be a dimension value, which is a floating point number appended with a unit such as “14.5sp”.
android:switchPadding setSwitchPadding(int) Minimum space between the switch and caption text Must be a dimension value, which is a floating point number appended with a unit such as “14.5sp”.
android:switchTextAppearance setSwitchTextAppearance(Context,int) TextAppearance style for text displayed on the switch thumb.
android:textOff setTextOff(CharSequence) Text to use when the switch is in the unchecked/”off” state.
android:textOn setTextOn(CharSequence) Text to use when the switch is in the checked/”on” state.
android:textStyle setSwitchTypeface(Typeface) Style (bold, italic, bolditalic) for the text.
android:thumb setThumbResource(int) Drawable to use as the “thumb” that switches back and forth.
android:thumbTextPadding setThumbTextPadding(int) Amount of padding on either side of text within the switch thumb.
android:thumbTint setThumbTintList(ColorStateList) Tint to apply to the thumb.
android:thumbTintMode setThumbTintMode(PorterDuff.Mode) Blending mode used to apply the thumb tint.
android:track setTrackResource(int) Drawable to use as the “track” that the switch thumb slides within.
android:trackTint setTrackTintList(ColorStateList) Tint to apply to the track.
android:trackTintMode setTrackTintMode(PorterDuff.Mode) Blending mode used to apply the track tint.
android:typeface setSwitchTypeface(Typeface) Typeface (normal, sans, serif, monospace) for the text.

状态监听

Switch mSwitch = (android.widget.Switch) findViewById(R.id.setting_switch);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
//选中时 do some thing
Toast.makeText(MainActivity.this, "enabled", Toast.LENGTH_SHORT).show();
} else {
//非选中时 do some thing
Toast.makeText(MainActivity.this, "disabled", Toast.LENGTH_SHORT).show();
}
}
});

设置开关状态显示的文字

<Switch
android:id="@+id/setting_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:showText="true"
android:textOff="关"
android:textOn="开" />

设置最小显示宽度

android:switchMinWidth="50dp"

最新文章

  1. less入门
  2. WebStorage记录滚动条位置
  3. 在 Angular 中实现搜索关键字高亮
  4. JNI测试-java调用c算法并返回java调用处-1到20阶乘的和
  5. cocos2dx 字体BMFont,Atlas
  6. 《windows程序设计》学习_3.1:画出雷区,左键的使用
  7. php 备份数据库
  8. python和C语言混编的几种方式
  9. MySQL NULL 值处理
  10. java之Spring(AOP)前奏-动态代理设计模式(上)
  11. arcEngine开发之加载栅格数据
  12. 静态代码扫描之阿里java代码规范IDEA插件
  13. 金蝶开k3wise客户端访问中间层不时提示远程服务器不存在或不可用
  14. 调整 Windows VM 的大小
  15. axios的配置项
  16. 如何进行Logstash logstash-input-jdbc插件的离线安装
  17. 【DB2】设置表不记录日志,但是回滚导致表不可用报错:DB2 SQL Error: SQLCODE=-1477, SQLSTATE=55019, SQLERRMC=LEN.TMP;5;3, DRIVER=3.57.82
  18. 51nod 1428 活动安排问题 (贪心+优先队列)
  19. ADO中记录集recordSet的使用
  20. [水煮 ASP.NET Web API2 方法论](1-2)在 WebForm 应用程序中添加 ASP.NET Web API

热门文章

  1. 使用requirejs来管理angularJS依赖示例
  2. 谈mysql优化
  3. LeetCode169:Majority Element(Hash表\位操作未懂)
  4. 【阿里聚安全&#183;安全周刊】互联网时代人类还有被遗忘的权利吗 | Android与中兴
  5. 不用第三方解码库取得图片宽高 附完整C++算法实现代码
  6. [ZJOI 2007]时态同步
  7. bzoj 1272: [BeiJingWc2008]Gate Of Babylon
  8. ●BZOJ 3545 [ONTAK2010]Peaks(离线)
  9. hdu 5645 DZY Loves Balls
  10. [ Java学习基础 ] Java构造函数