Android自定义开关Switch

image.png

Java部分,监听和调用

ToolSwitch.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>

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

推荐阅读更多精彩内容