博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Switch控件详解
阅读量:6477 次
发布时间:2019-06-23

本文共 2476 字,大约阅读时间需要 8 分钟。

Switch控件详解

原生效果

5.x

这里写图片描述

4.x

这里写图片描述

布局

属性

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();        }    }});

设置开关状态显示的文字

P4

设置最小显示宽度

android:switchMinWidth="50dp"

转载于:https://www.cnblogs.com/sesexxoo/p/6190431.html

你可能感兴趣的文章
python 生成器
查看>>
mybatis的mapper.xml使用parameterType使用的报错
查看>>
HTTPS(SSL)详解以及PHP调用方法
查看>>
突发小事件,USB接口问题
查看>>
适合wordpress中文网站的seo优化插件 DX-Seo
查看>>
Nginx负载均衡配置实例详解
查看>>
L1-009. N个数求和
查看>>
2016 Multi-University Training Contest 2 部分题解
查看>>
jsp页面可以巧用模态框
查看>>
Eclipse常用快捷键
查看>>
实参传递不当导致的运行时错误
查看>>
PHP生成静态html文件 的三种方法
查看>>
leetcode-796-Rotate String
查看>>
sqlserver 批量删除存储过程(转)
查看>>
微信小程序 setData 的坑(转)
查看>>
javascript 阻塞
查看>>
PHP 使用共享内存的资料
查看>>
getLayoutInflater().inflate .
查看>>
CSS3制作心形头像
查看>>
app开发-1
查看>>