Android 主题属性部分在浅色和深色之间切换

Android theme properties partially switching between light and dark colors

我正在为我的应用构建一个 Light/Dark 主题。我想为应用程序中的所有按钮使用自定义形状。但是在主题之间交换时,colorAccent 属性不会交换为按钮背景颜色。

灯光模式下,colorAccent橙色,显示橙色。

但在深色模式下,colorAccent紫色,但它仍然显示橙色

我知道交换工作正常,因为我可以更改其他颜色并且它们会被采用。这只是按钮的形状。

我确定它与形状文件中的 <solid android:color="@color/colorAccent" /> 有关。

谁能看出我做错了什么?

浅色主题

<resources>
   <!--Top level DayNight theme to be used in AndroidManifest.xml-->
    <style name="MyCustomTheme" parent="Base.MyCustomTheme"/>

    <style name="MyCustomTheme.System.Defaults" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <item name="android:fontFamily">@font/driver_font_family</item>
    </style>


    <style name="Base.MyCustomTheme" parent="MyCustomTheme.System.Defaults">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@color/OffWhite</item>

        <!--Component styles-->
        <item name="buttonStyle">@style/MyCustomTheme.Button</item>
    </style>

</resources>

夜间主题

<resources>
    <style name="MyCustomTheme" parent="Base.MyCustomTheme">
        <item name="colorPrimary">@color/Green</item>
        <item name="colorPrimaryDark">@color/Red</item>
        <item name="colorAccent">@color/Purple</item>
        <item name="android:windowBackground">@color/BlackDark</item>
    </style>
</resources>

rounded_corners.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners  android:radius="10dp" />
    <padding  android:padding="0dp"/>
    <solid android:color="@color/colorAccent" />
</shape>

colors.xml

<resources>
    <!--Leaving these here since they are refrenced by the system and other components-->
    <color name="colorPrimary">@color/NavyBlue</color>
    <color name="colorPrimaryDark">@color/NavyBlueDark</color>
    <color name="colorAccent">@color/Orange</color>

    <color name="Red">#E84E3C</color>
    <color name="Purple">#745EC4</color>
    <color name="Green">#2FCC70</color>
    <color name="NavyBlue">#34495E</color>
    <color name="NavyBlueDark">#2B3D4F</color>
    <color name="BlueDark">#394D82</color>    
    <color name="OffWhite">#EDF1F2</color>
    <color name="BlackDark">#262626</color>
</resources>

问题在这里:

<solid android:color="@color/colorAccent" />

通过这种方式,您可以 link 使用 colors.xml(即 @color/Orange)中定义的 颜色。相反,您应该 link 应用主题中定义的 属性

更改为:

<solid android:color="?attr/colorAccent" />