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. 关于if后面直接加上参数名,不加条件的用法

    <template> <section> <p v-if="aa">{{aa}}</p> <p v-if="bb&q ...

  2. C#之IComparable用法,实现List<T>.sort()排序

    这篇文章主要介绍了C#的一些基础知识,主要是IComparable用法,实现List<T>.sort()排序,非常的实用,这里推荐给大家.   List<T>.sort()可以 ...

  3. vue中自定义组件(插件)

    vue中自定义组件(插件) 原创 2017年01月04日 22:46:43 标签: 插件 在vue项目中,可以自定义组件像vue-resource一样使用Vue.use()方法来使用,具体实现方法: ...

  4. [ZJOI 2006]物流运输

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  5. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. ●BZOJ 4559 [JLoi2016]成绩比较

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4559 题解: 计数dp,拉格朗日插值法.真的是神题啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 ...

  7. 从零安装Scrapy心得 | Install Python Scrapy from scratch

    1. 介绍 Scrapy,是基于python的网络爬虫框架,它能从网络上爬下来信息,是data获取的一个好方式.于是想安装下看看. 进到它的官网,安装的介绍页面 https://docs.scrapy ...

  8. Saltstack基础

    salt介绍 salt简单介绍 一个基础平台管理工具 一个配置管理系统,能够维护预定义状态的远程节点 一个分布式远程执行系统,用来在远程节点上执行命令和查询数据 salt的核心功能 使命令发送到远程系 ...

  9. decode-ways(动态规划)

    题目描述 A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A ...

  10. Python中模块之xml的讲解

    xml模块的功能介绍 这里主要讲解xml模块下的etree.ElementTree类. 1. 创建 具体代码如下 import xml.etree.ElementTree as XM namelist ...