SwitchCompat 在深色模式下显示效果不佳

SwitchCompat doesn't display well in dark mode

我有一个自定义 SwitchCompat,它在应用程序的白天主题中呈现得很好,但是当启用深色主题时,SwitchCompat 看起来太亮了。这是一张照片:

开关兼容性:

    <androidx.appcompat.widget.SwitchCompat
    android:id="@+id/alarm_switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:background="@null"
    android:thumb="@drawable/sw_thumb"
    app:track="@drawable/sw_track"/>

sw_thumb.xml:

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

<item android:state_checked="true">
    <shape android:shape="oval">
        <solid android:color="@color/white"/>
        <size android:width="25dp" android:height="25dp"/>
        <stroke android:width="2dp" android:color="@color/main_color"/>
    </shape>
</item>

<item android:state_checked="false">
    <shape android:shape="oval">
        <solid android:color="@color/white"/>
        <size android:width="25dp" android:height="25dp"/>
        <stroke android:width="2dp" android:color="@color/gray"/>
    </shape>
</item>

</selector>

sw_track.xml:

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

<item android:state_checked="true">
    <shape android:shape="rectangle">
        <solid android:color="@color/main_color"/>
        <corners android:radius="200dp"/>
    </shape>
</item>

<item android:state_checked="false">
    <shape android:shape="rectangle">
        <solid android:color="@color/gray"/>
        <corners android:radius="200dp"/>
    </shape>
</item>

</selector>

如果要自定义颜色,再创建一个同名的颜色文件 values->colours->右键单击,New->Values Resource File->Available qualifiers ->向下滚动到 Night Mode ->give File name as same

然后更改夜间模式下的颜色代码,使其成为夜间模式。