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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(isChecked) {//选中时 do some thingToast.makeText(MainActivity.this, "enabled",  Toast.LENGTH_SHORT).show();} else {//非选中时 do some thingToast.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"

Switch控件详解相关推荐

  1. android什么控件能够输入多行文字,Android开发:文本控件详解——EditText(一)基本属性...

    一.简单实例: EditText输入的文字样式部分的属性,基本都是和TextView中的属性一样. 除此之外,EditText还有自己独有的属性. 二.基本属性: hint 输入框显示的提示文本 te ...

  2. 【转】ASP.NET验证控件详解(非空验证,比较验证,范围验证,正则表达式,自定义验证)...

    [转]ASP.NET验证控件详解(非空验证,比较验证,范围验证,正则表达式,自定义验证) ASP.NET验证控件详解 现在ASP.NET,你不但可以轻松的实现对用户输入的验证,而且,还可以选择验证在服 ...

  3. 常用数据绑定控件详解

    常用数据绑定控件详解 GridView内容详解(1) GridView内容详解(2) DataList内容详解 ListView详解(一) ListView详解(二) 导航控件的高级应用 from:h ...

  4. android自定义组件属性,Android组合控件详解 自定义属性

    组合控件详解 & 自定义属性 组合控件是自定义控件的一种,只不过它是由其他几个原生控件组合而成,故名组合控件. 在实际项目中,GUI 会遇到一些可以提取出来做成自定义控件情况. 一个自定义控件 ...

  5. pythongui日历控件_“五一”快到了,用Python中PyQt5做一个日历,QCalendar控件详解...

    前面内容,我们详细了解了PyQt5中的一些常用控件 精彩内容回顾 Python用PyQt5设计界面,如何正确显示一幅图片,QPixmap控件详解 拥有漂亮的笔刷才能绘制多彩界面,PythonPyQt5 ...

  6. WindowsMediaPlayer 11 控件详解

    WindowsMediaPlayer 11 控件详解 转载别人的VB内容 C#中大多也通用 都是Microsoft的作品 . 属性/方法名: 说明: [基本属性] URL:String; 指定媒体位置 ...

  7. Spinner控件详解

    Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...

  8. Qt Quick - Popup控件详解

    Qt Quick - Popup控件详解 Popup是基于Qt Quick中的一种弹出式用户界面控件.它可以和Window或ApplicationWindow一起使用.如果想要确保一个Popup在场景 ...

  9. 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)

    博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...

最新文章

  1. javascript优缺点_为什么要在JavaScript中使用静态类型? 优缺点
  2. [JS]格式化Date和分析格式化Date字符串的JS代码
  3. 加载NMGameX.dll时出错?
  4. FIR数字滤波器设计频率抽样法MATLAB仿真
  5. 模拟网页行为之实践四
  6. mysql phpmyadmin远程_phpmyadmin登录远程mysql数据库
  7. 雷军:智能制造将进一步带动中国企业在全球市场的全面崛起
  8. coreldraw登录老是出现未知错误_原神:新版本bug频出,史莱姆消失,联机模式提示506错误码...
  9. 学会科学饮水令你越喝越美
  10. DoraHacks的笔记
  11. apollo代码修改配置_灰度实战(四):Apollo配置中心(4)
  12. vector容器易错知识点集锦
  13. mysql内容_mysql 的基本内容
  14. XP安全防护国际评测 360小红伞卡巴排行前三
  15. 图像风格化——感知损失(perceptual loss)(2016)
  16. 计算机教学模式有待创新,计算机软件教学中教学评价模式的创新与探索
  17. 恺撒密码算法 Java实现
  18. ubuntu折腾笔记【三】
  19. android 音频播放过程,一种Android系统中的音频播放方法与流程
  20. Richard Stallman 的自由软件思想

热门文章

  1. 1386. 安排电影院座位
  2. 【数据挖掘】之 数据挖掘 绪论
  3. apk分享: Android应用更换皮肤功能的实现思路教程。
  4. gog无效的验证码_如何将您所有的PC游戏库与GOG Galaxy合并
  5. Photoshopcs6 自学笔记三 画笔工具
  6. win10与ubuntu双系统安装
  7. 计算机怎么关闭开机自启应用,如何关闭电脑开机自动启动的软件程序
  8. Android7工程模式,安卓手机怎么进工程模式 安卓手机进工程模式教程【详解】
  9. 悬链线锚链力的求解方法
  10. c语言_调和级数不等式