美文网首页
Android自定义开关Switch

Android自定义开关Switch

作者: Amy木婉清 | 来源:发表于2021-02-27 13:49 被阅读0次
    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>
    
    

    相关文章

      网友评论

          本文标题:Android自定义开关Switch

          本文链接:https://www.haomeiwen.com/subject/mrmsfltx.html