PreferenceScreen中的ColorAccent在API <= 22

时间:2019-02-05 19:00:09

标签: java android xml styles android-preferences

我在模拟器上安装了 API 22 ,并注意到ListPreferences和Preferences中的RadioButtons和Buttons并未更改为样式文件中指定的正确颜色。在API> 22上可以正常工作。此问题仅发生在SettingsActivity中使用的PreferenceScreen中。

我想要的颜色是#76B900(绿色)

我得到的是默认的 Android浅蓝色颜色

RadioButton

Button

主题已在清单中注册:

        <activity
            android:name="com.Test.App.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:windowSoftInputMode="adjustPan"
            android:theme="@style/PreferencesTheme"
            android:screenOrientation="portrait"/>

首选项主题(styles.xml):

<style name="PreferencesTheme" parent="Theme.AppCompat">
        <item name="android:windowBackground">@color/background</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/primaryText</item>
        <item name="android:textColorSecondary">@color/settingsSubtext</item>
        <item name="android:colorBackground">@color/background</item>
        <item name="android:textColorPrimaryInverse">@color/primaryText</item>
    </style>

首选项主题(styles.xml(v21)):

<style name="PreferencesTheme" parent="Theme.AppCompat">
        <item name="android:windowBackground">@color/background</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/primaryText</item>
        <item name="android:textColorSecondary">@color/settingsSubtext</item>
        <item name="android:colorBackground">@color/background</item>
        <item name="android:textColorPrimaryInverse">@color/primaryText</item>
        <item name="android:colorAccent">@color/colorAccent</item>
    </style>

颜色(colors.xml):

    <color name="colorPrimary">#0F0F0F</color> - Black
    <color name="colorPrimaryDark">#000000</color> - Black
    <color name="colorPrimaryLight">#C8E6C9</color> - Light Green
    <color name="colorAccent">#76B900</color> - Green
    <color name="primaryText">#FFFFFF</color> - White
    <color name="secondaryText">#757575</color> - Gray
    <color name="background">#333333</color> - Dark Gray

颜色或样式文件中的任何地方均未设置默认的Android浅蓝色。

1 个答案:

答案 0 :(得分:1)

我有一个类似的问题。

将此添加到样式文件中:

<style name="AlertDialogCustom" parent="android:Theme.Material.Dialog.Alert">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:colorAccent">@color/colorAccent</item>
</style>

将此添加到您的PreferenceTheme样式中:

<item name="android:alertDialogTheme">@style/AlertDialogCustom</item>

按钮现在应该具有正确的colorAccent颜色。