Switch控件详解
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控件详解的更多相关文章
- ToolBar控件详解
ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat ...
- IOS—UITextFiled控件详解
IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...
- picker控件详解与使用,(实现省市的二级联动)
picker控件详解与使用,(实现省市的二级联动) 第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试, ...
- Spinner控件详解
Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...
- Android开发:文本控件详解——TextView(一)基本属性
一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...
- Android开发:文本控件详解——TextView(二)文字跑马灯效果实现
一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...
- C++ CComboBox控件详解
转载:http://blog.sina.com.cn/s/blog_46d93f190100m395.html C++ CComboBox控件详解 (2010-09-14 14:03:44) 转载▼ ...
- 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)
博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...
- 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )
转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...
随机推荐
- [JCIP笔记](四)踩在巨人的肩上
读完第三章那些繁琐的术语和细节,头疼了整整一个星期.作者简直是苦口婆心,说得我如做梦一般.然而进入第四章,难度骤然降低,仿佛坐杭州的过山公交车突然下坡,鸟鸣花香扑面而来,看到了一片西湖美景. 从开始看 ...
- ASP.NET MVC5 使用NPOI导出ExceL 返回浏览器下载
一,什么是NPOI 该项目是位于http://poi.apache.org/的POI Java项目的.NET版本.POI是一个开源项目,可以帮助您读取/写入xls,doc,ppt文件.它有着广泛的应用 ...
- CodeForces 912d fishes(优先队列+期望)
While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of ...
- JDK安装、变量、变量的分类
Lesson One 2018-04-17 19:50:35 JAVA语言特点: 编译型.强类型语言. 纯面向对象的语言,所有的代码都必须包含在class中的方法中 配置JAVA环境变量 1.安装J ...
- C#之winform实现文件拖拽功能
将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了 将一个控件的属性AllowDrop设置为true,然后添加DragDrop.DragEnter ...
- python 简单实现淘宝关键字商品爬取
本文有2个文件 1:taobao_re_xpath 2:taobao_re_xpath_setting # 1:taobao_re_xpath # -*- coding:utf-8 -*- # aut ...
- [NOI 2014]魔法森林
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
- [HNOI 2003]消防局的设立
Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来 连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成 ...
- Uva 437 巴比伦塔 && UVA10003
要求底面严格小于它下方立方体的长宽,求出最高情况,一块石头可以多次使用 用结构体记录一块石头的三种放置情况,按面积排序. dp[i] = max(dp[i],dp[j] + block[i].high ...
- [BZOJ]2194: 快速傅立叶之二
题目大意:给定序列a,b,求序列c满足c[k]=sigma(a[i]*b[i-k]) (k<=i<n).(n<=10^5) 思路:观察发现就是普通的卷积反一反(翻转ab其中一个后做卷 ...