Android Switch和ToggleButton控件

1. Switch类

Switch类被用来展示状态。

  • android:checked设置是否打开
    这里写图片描述

自定义Switch

  • android:track设置背景
  • android:thumb设置选择背景
  • android:switchMinWidth设置宽度

背景文件switch_track.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <size android:height="30dp" />
            <corners android:radius="15dp"/>
            <solid android:color="#ff8b8b8b" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <size android:height="30dp" />
            <corners android:radius="15dp"/>
            <solid android:color="#ffff8c00" />
        </shape>
    </item>
</selector>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

选择背景文件switch_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <size android:height="30dp" android:width="30dp" />
            <corners android:radius="15dp"/>
            <solid android:color="#ffffffff" />
            <stroke android:width="1dp" android:color="#ff8b8b8b" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <size android:height="30dp" android:width="30dp" />
            <corners android:radius="15dp"/>
            <solid android:color="#ffffffff" />
            <stroke android:width="1dp" android:color="#ffff8c00" />
        </shape>
    </item>
</selector>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Switch引用自定义样式

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:switchMinWidth="20dp"
    android:track="@drawable/switch_track"
    android:thumb="@drawable/switch_thumb"
    android:checked="true"/>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

效果如下
这里写图片描述

2. ToggleButton类

  • android:textOn设置选中时文本
  • android:textOff设置取消选中时文本

配置文件

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="关闭"
    android:textOn="打开"
    android:checked="true" />

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

效果如下
这里写图片描述

参考资料:http://blog.youkuaiyun.com/qq_34763699/article/details/54954394

相关文章
Android RadioButton和CheckBox控件
Android Switch和ToggleButton控件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值