Switch控件详解

简介: Switch控件详解原生效果5.x4.x布局属性Attribute Name Related Method Descri...

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

设置开关状态显示的文字

P4

<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"
相关文章
|
1月前
|
程序员 C# 图形学
【Unity 3D】C#中条件语句if else switch的讲解
【Unity 3D】C#中条件语句if else switch的讲解
83 1
|
24天前
|
Java Android开发 开发者
17. 【Android教程】开关控件ToggleButton/Switch
17. 【Android教程】开关控件ToggleButton/Switch
19 2
|
11月前
|
iOS开发
自定义Switch控件
做iOS开发也有一段时间的了,项目中的开关控件一直都是用的系统级别的,至多就是给UISwitch控件换一个tintColor。这次的UI设计师设计了一个带有动画效果的UISwitch控件。
自定义Switch控件
|
1月前
|
XML Android开发 数据格式
[Android]开关控件Switch
[Android]开关控件Switch
41 0
uniapp switch按钮的使用开关按钮效果demo(整理)
uniapp switch按钮的使用开关按钮效果demo(整理)
|
Python
小白白也能学会的 PyQt 教程 —— 自定义组件 Switch Button
最近在搞 Python 课程设计,想要搞一个好看的 UI,惊艳全班所有人。但打开 Qt Creator,Win7 风格的复古的按钮是在让我难以下手。其次,我因为想要打造一个 Fluent UI 样式的设置页面,需要一个好看的 Switch Button,来用于设置界面部分设置项的转换,于是便决定动手写一个;
8072 1
|
存储 C# 索引
【UiPath2022+C#】UiPath Switch
这是一种控制流语句,可以根据特定表达式的值执行多组语句中的一组。换句话说,如果我们至少需要处理 3 种可能的操作过程,则可以使用切换而是 If 条件语句。这可以通过条件判断来实现,就像在 If 条件语句中一样,只不过它的条件不是布尔值,而是多个值。
【UiPath2022+C#】UiPath Switch
|
前端开发
autojs自定义控件Switch
牙叔教程 简单易懂
438 0
写一个原生switch开关按钮
欢迎阅读本博文,本博文主要讲述【写一个原生switch开关按钮】,文字通俗易懂,如有不妥,还请多多指正。
写一个原生switch开关按钮
|
Android开发
Android Switch控件修改样式
Android Switch控件修改样式
Android Switch控件修改样式