switch 开关 与自定义

267 阅读1分钟

原文地址: blog.csdn.net/kong_gu_you…

本文主旨在记录switch的使用,功能小,但值得记录。主要实现自定义按钮、轨道、文字、文字颜色。切换动画这块原生效果已经实现了切换效果,基本够用,不在记录范围。

1.原生效果

<Switch 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />

官方API效果

image.png

2.自定义

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:thumb="@drawable/switch_custom_thumb_selector"
    android:track="@drawable/switch_custom_track_selector" />

效果图

image.png

2.1自定义按钮-thumb

开启状态:switch_custom_thumb_on.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#FFF" />
    <stroke android:color="#353535"
        android:width="1px"
        />
    <size
        android:width="20dp"
        android:height="20dp" />
</shape>

关闭状态:switch_custom_thumb_off.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/back_color_gray" />
    <stroke android:color="#353535"
        android:width="1px"
        />
    <size
        android:width="20dp"
        android:height="20dp" />
</shape>

定义一个selector:switch_custom_thumb_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_custom_thumb_on" android:state_checked="true" />
    <item android:drawable="@drawable/switch_custom_thumb_off" android:state_checked="false" />
</selector>

2.2自定义轨道-track

开启状态,shape_switch_track_on.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#4E78Dc" />
    <stroke
        android:width="5dp"
        android:color="#00000000" />

    <corners android:radius="20dp" />
</shape>

关闭状态,shape_switch_track_off.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#E3E3E3" />
    <stroke
        android:width="5dp"
        android:color="#00000000" />
    <corners android:radius="20dp" />
</shape>

定义selector,selector_switch_track.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_switch_track_on" android:state_checked="true" />
    <item android:drawable="@drawable/shape_switch_track_off" android:state_checked="false" />
</selector>

按钮突出进度条

设置track透明边框即可

2.3自定义文字-switchTextAppearance

textOn和textOff属性可以分别设置开启和关闭的文字,别忘了将showText属性设置为true,这样才能显示出来:

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:showText="true"
    android:switchTextAppearance="@style/SwitchTheme"
    android:textOff="OFF"
    android:textOn="ON"
    android:thumb="@drawable/switch_rectangle_thumb_selector"
    android:track="@drawable/switch_rectangle_track" />

修改文字的颜色:

在res文件夹下建一个color文件夹,定义一个文本颜色状态的selector:switch_text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#FFF" android:state_checked="false" />
    <item android:color="#000" android:state_checked="true" />
</selector>

然后在style文件中定义一个样式:

<style name="SwitchTheme" parent="@android:style/TextAppearance.Small">
    <item name="android:textColor">@color/switch_text_selector</item>
</style>

最后在Switch中设置一下就可以了:

android:switchTextAppearance="@style/SwitchTheme"