Switch控件详解

Switch控件详解

原生效果

5.x

这里写图片描述

4.x

这里写图片描述

布局

<Switch
    android:id="@+id/setting_switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

属性

Attribute NameRelated MethodDescription
android:showTextsetShowText(boolean)Whether to draw on/off text.
android:splitTracksetSplitTrack(boolean)Whether to split the track and leave a gap for the thumb drawable.
android:switchMinWidthsetSwitchMinWidth(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:switchPaddingsetSwitchPadding(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:switchTextAppearancesetSwitchTextAppearance(Context,int)TextAppearance style for text displayed on the switch thumb.
android:textOffsetTextOff(CharSequence)Text to use when the switch is in the unchecked/”off” state.
android:textOnsetTextOn(CharSequence)Text to use when the switch is in the checked/”on” state.
android:textStylesetSwitchTypeface(Typeface)Style (bold, italic, bolditalic) for the text.
android:thumbsetThumbResource(int)Drawable to use as the “thumb” that switches back and forth.
android:thumbTextPaddingsetThumbTextPadding(int)Amount of padding on either side of text within the switch thumb.
android:thumbTintsetThumbTintList(ColorStateList)Tint to apply to the thumb.
android:thumbTintModesetThumbTintMode(PorterDuff.Mode)Blending mode used to apply the thumb tint.
android:tracksetTrackResource(int)Drawable to use as the “track” that the switch thumb slides within.
android:trackTintsetTrackTintList(ColorStateList)Tint to apply to the track.
android:trackTintModesetTrackTintMode(PorterDuff.Mode)Blending mode used to apply the track tint.
android:typefacesetSwitchTypeface(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"

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值