Android Switch开关自定义

先不废话,上图

可以自定义滑块 和背景哦~


未选中
已选中

2、开始进入代码环节

Java部分,监听和调用

mToolSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                   showShortToast("选中");
                }else {
                    showShortToast("没选中");
                }
            }
        });

3、switch控件属性介绍

android:showText:设置on/off的时候是否显示文字,boolean
android:splitTrack:是否设置一个间隙,让滑块与底部图片分隔,boolean
android:switchMinWidth:设置开关的最小宽度
android:switchPadding:设置滑块内文字的间隔
android:switchTextAppearance:设置开关的文字外观,暂时没发现有什么用...
android:textOff:按钮没有被选中时显示的文字
android:textOn:按钮被选中时显示的文字
android:textStyle:文字风格,粗体,斜体写划线那些
android:track:底部的图片
android:thumb:滑块的图片
android:typeface:设置字体

4、进入正题 xml部分代码编写如下

<Switch
                    android:id="@+id/tool_switch"
                    android:textColor="@color/fsk_subtitle_black"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:track="@drawable/switch_selector_green"
                    android:thumb="@drawable/switch_white_circle_selector"
                    android:text="开关"
                    />

5、样式编写代码如下

switch_selector_green 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/switch_bg_selected" android:state_checked="true"/>
    <item android:drawable="@drawable/switch_bg_normal" android:state_checked="false"/>

</selector>


switch_bg_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <solid android:color="@color/fsk_green"/>
    <size  android:width="40dp"
           android:height="24dp"/>
    <corners android:radius="20dp"/>

</shape>

switch_bg_normal
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <solid android:color="@color/fsk_base_bg"/>
    <size  android:width="40dp"
           android:height="24dp"/>
    <corners android:radius="20dp"/>

</shape>

switch_white_circle_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/switch_white_circle_selected" android:state_checked="true"/>
    <item android:drawable="@drawable/switch_white_circle_normal" android:state_checked="false"/>

</selector>

switch_white_circle_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <stroke android:width="1dp" android:color="@color/fsk_green"/>
    <solid android:color="@color/white"/>
    <size  android:width="24dp"
           android:height="24dp"/>

</shape>

switch_white_circle_normal
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <stroke android:width="1dp" android:color="@color/fsk_base_bg"/>
    <solid android:color="@color/white"/>
    <size  android:width="24dp"
           android:height="24dp"/>

</shape>

样式搞定 ,当然了 要是shape实现不了,就用图片咯!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前些天,在父母的逼迫下见了个女孩子,不喜欢也不讨厌,在亲戚的淫威下留了号码,加了微信,本不想多聊,但父母天天电话相...
    烟程阅读 176评论 0 0
  • 别再等到 春天才去盛开 你若盛开 每一刻都是春天 别再等到 凋零才去采摘 你若采摘 每一朵都要鲜美 别再等到 熟识...
    鲁盾阅读 251评论 0 2
  • 曾经的疯狂 曾经的辉煌 属于我们一代人的记忆
    Olive_01阅读 537评论 0 2
  • 课程表上竟然没有安排固定的作文课,办公室的语文老师就商定,连着的两节语文自主上成作文。 关于七年级第一次作文写什么...
    改变自己369阅读 858评论 0 6