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 ...
随机推荐
- 算法题丨Next Permutation
描述 Implement next permutation, which rearranges numbers into the lexicographically next greater perm ...
- MySql 使用规范推荐
前言 废话不多说-- 一.基础规范 1.使用InnoDB存储引擎 支持事务.行级锁.并发性能更好.CPU及内存缓存页优化使得资源利用率更高 2.推荐使用utf8mb4字符集 无需转码,无乱码风险, 支 ...
- Headless Chrome:服务端渲染JS站点的一个方案【中篇】【翻译】
接上篇 防止重新渲染 其实说不对客户端代码做任何修改是忽悠人的.在我们的Express 应用中,通过Puppteer加载页面,提供给客户端响应,但是这个过程是有一些问题的. js脚本在服务端的Head ...
- [测试题]gentree
Description 给你一个有向连通图G,每点有个权值Di(0<Di),要求生成一棵树根为1号节点的有根树T.对于树中边E,E的代价为所有从根出发的且包含E的路径的终点权值的和.现求生成树T ...
- 【bzoj4569 scoi2016】萌萌哒
题目描述 一个长度为n的大数,用S1S2S3...Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条件表示为四个数,l1,r1,l2,r2,即两个长度相同的区间,表示子串S ...
- hdu 5112 (2014北京 水)
题意:有个人在跑步,一直每个时间他所在的位置,求最大速度 #include <iostream> #include <cstring> #include <cstdio& ...
- 例10-6 uva1635(唯一分解定理)
题意:给定n个数a1,a2····an,依次求出相邻两个数值和,将得到一个新数列,重复上述操作,最后结果将变为一个数,问这个数除以m的余数与那些数无关? 思路:最后观察期规律符合杨辉三角,那么,问题就 ...
- 软件测试人员在工作中如何运用Linux
从事过软件测试的小伙们就会明白会使用Linux是多么重要的一件事,工作时需要用到,面试时会被问到,简历中需要写到. 对于软件测试人员来说,不需要你多么熟练使用Linux所有命令,也不需要你对Linux ...
- 使用C# (.NET Core) 实现状态设计模式 (State Pattern)
本文的概念性内容来自深入浅出设计模式一书 项目需求 这是一个糖果机的需求图. 它有四种状态, 分别是图中的四个圆圈: No Quarter: 无硬币 Has Quater 有硬币 Gumball So ...
- Python的IO编程
原文传送门:请点击 原文传送门:请点击