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"

Switch控件详解的更多相关文章

  1. ToolBar控件详解

    ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat ...

  2. IOS—UITextFiled控件详解

    IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...

  3. picker控件详解与使用,(实现省市的二级联动)

    picker控件详解与使用,(实现省市的二级联动) 第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试, ...

  4. Spinner控件详解

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

  5. Android开发:文本控件详解——TextView(一)基本属性

    一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...

  6. Android开发:文本控件详解——TextView(二)文字跑马灯效果实现

    一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...

  7. C++ CComboBox控件详解

    转载:http://blog.sina.com.cn/s/blog_46d93f190100m395.html C++ CComboBox控件详解 (2010-09-14 14:03:44) 转载▼ ...

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

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

  9. 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )

    转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...

随机推荐

  1. Ubuntu16.04开机引导缺失Win10

    Ubuntu正常开机的情况下: sudo update-grub # 如果grub丢失, 就先sudo apt install grub Ubuntu不能正常开下: 进入Ubuntu引导, 不要正常进 ...

  2. 招募:Wiki 文档翻译小伙伴招募

    https://github.com/dotnetcore/CAP/issues/93 为了推进 CAP 的国际化工作,为全球其他 .NET 开发者提供更加良好的文档阅读体验,现在需要对CAP wik ...

  3. python 元组(tuple)的使用方法介绍

    一.元组定义 元组和列表类似,元组使用的是小括号,列表是中括号,但是元组不像列表那样可以增删改:如果列表中存在列表或字符串,那么可以对其进行修改. 创建一个元组,只需要括号中添加元素,元素用逗号隔开即 ...

  4. UWP中的文件相关操作

    最近开始做UWP开发,图省事儿就把自己之前一个Winform项目的一部分代码拷贝到了新写的UWP项目中来.整出了一些幺蛾子,下面做一个记录. 首先提一个重点就是:UWP里关于文件的操作尽量用Stora ...

  5. Redis事务管理

    用过其他关系型数据库(比如msql)的肯定都指定,在关系型数据库里面的事务可以保证多个命令操作要么同时成功,要么同时失败.并且在执行事务的时候,可以有隔离级别. 但是在Redis中的事务,只是保证事务 ...

  6. 《C++ Primer》学习笔记:迭代器介绍

    <C++Primer>(第五版)中,3.4.1的例题中使用一个名为text的字符串向量存放文本文件中的数据,输出text中的内容,刚开始我这样写: #include <iostrea ...

  7. codefroces 911G Mass Change Queries

    题意翻译 给出一个数列,有q个操作,每种操作是把区间[l,r]中等于x的数改成y.输出q步操作完的数列. 输入输出格式 输入格式: The first line contains one intege ...

  8. [Apio2009]Atm

    题目描述 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每 ...

  9. Topcoder口胡记 SRM 562 Div 1 ~ SRM 599 Div 1

    据说做TC题有助于提高知识水平? :) 传送门:https://284914869.github.io/AEoj/index.html 转载请注明链接:http://www.cnblogs.com/B ...

  10. ●UVa 11346 Probability

    题链: https://vjudge.net/problem/UVA-11346题解: 连续概率,积分 由于对称性,我们只用考虑第一象限即可. 如果要使得面积大于S,即xy>S, 那么可以选取的 ...