android Switch 修改颜色 修改样式 滑块颜色

设置switch颜色的四个属性: 

    <androidx.appcompat.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:trackTint="#aaaaaa"
        app:trackTintMode="multiply"
        app:thumbTint="@color/colorPrimary"
        app:thumbTintMode="multiply" />

控制滑块颜色

        app:thumbTint="……"
        app:thumbTintMode="……"

其中thumbTint设置滑块的资源,thumbTimtMode设置滑块的染色模式

同理,滑道颜色也有两个属性,分别设置滑道资源和染色模式

        app:trackTint="……"
        app:trackTintMode="……"

Switch是Checkable的子类,有check属性,同样可以分别设置开关状态的颜色

以设置滑块颜色为例,在res包下新建color包,然后新建文件:switch_thumb_color.xml

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

设置滑块资源

    <androidx.appcompat.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:thumbTint="@color/switch_thumb_color"
        app:thumbTintMode="multiply"
        android:checked="true"/>

猜你喜欢

转载自blog.csdn.net/jingzz1/article/details/110229105