将颜色更改为设置组之间的分隔线

Change color to dividers between settings groups

我的 preferences.xml 文件如下所示:

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

<android.support.v7.preference.PreferenceCategory
    android:key="themeCategoryKey"
    android:title="Theme">

    <SwitchPreferenceCompat
        android:key="themeKey"
        android:title="Turn on dark theme" />

</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory
    android:key="locationKeyCategory"
    android:title="Location">

    <EditTextPreference
        android:name="EditText Preference2"
        android:key="locationKey"
        android:summary="Enter default location for home screen"
        android:title="Location" />

</android.support.v7.preference.PreferenceCategory>

</PreferenceScreen>

我想要实现的是更改偏好组之间分隔线的颜色。我需要它,因为当我将主题颜色从深色更改为具有白色背景色的浅色主题时,分隔线不明显,因为它们是白色的,尽管分隔线在深色主题中很明显,因为背景色是黑色。在接下来的部分中,我提供了 style.xml。

<resources>

<!-- Dark Theme -->
<style name="BlackTheme" parent="Theme.AppCompat">
    <item name="colorPrimaryDark">@color/backgroundDarkTheme</item>
    <item name="colorPrimary">#171717</item>
    <item name="colorAccent">@color/colorAccentDarkTheme</item>
    <item name="android:windowBackground">@color/colorPrimaryDarkerDark</item>
    <item name="android:itemBackground">@color/colorPrimaryDarkerDark</item>
    <item name="android:textColor">@color/textColorDarkTheme</item>
    <item name="android:divider">@null</item>
</style>

<!-- Orange Theme Default One -->
<style name="OrangeTheme" parent="Theme.AppCompat">  <!-- this is light theme -->
    <item name="colorPrimaryDark">#e95304</item>
    <item name="colorPrimary">@color/colorAccentOrangeTheme</item>
    <item name="colorAccent">@color/colorAccentOrangeTheme</item>
    <item name="android:windowBackground">@color/backgroundOrangeTheme</item>
    <item name="android:itemBackground">@color/toolbarItemBackgroundLight</item>
    <item name="android:itemTextAppearance">@style/menu_item_color</item>
    <item name="android:textColor">@color/textColorOrangeTheme</item>
    <item name="android:textColorSecondary">@color/textColorOrangeTheme</item>
    <item name="android:divider">@color/textColorOrangeTheme</item>  <!-- this is black color btw -->
</style>

<!-- Menu Item Text Color Orange Theme -->
<style name="menu_item_color">
    <item name="android:textColor">@color/backgroundOrangeTheme</item>
</style>

</resources>

我在这里试过了,但没有成功。

  <item name="android:divider">@color/textColorOrangeTheme</item>

希望你们明白我打算做什么以及我怎样才能实现我的目标。对于那些不确切知道我想要设计哪个分隔线样式的人,图片在下方

这是没有分隔线的图片

你能试试这个,代替你的 styles.xml 中的 'OrangeTheme' 吗?这对我有用,请检查这是否是您需要的?

<style name="OrangeTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimaryDark">#e95304</item>
        <item name="colorPrimary">@color/colorAccentOrangeTheme</item>
        <item name="colorAccent">@color/colorAccentOrangeTheme</item>
        <item name="android:itemTextAppearance">@style/menu_item_color</item>
        <item name="android:textColor">@color/textColorOrangeTheme</item>
        <item name="android:textColorSecondary">@color/textColorOrangeTheme</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
    </style>

要自定义分隔线,您需要将 SettingsTheme 应用到您的设置 activity。

<style name="SettingsTheme" parent="Theme.AppCompat">
    <item name="preferenceTheme">@style/PreferenceTheme</item>
</style>

<style name="PreferenceTheme" parent="PreferenceThemeOverlay">
    <item name="preferenceFragmentCompatStyle">@style/FragmentPreferenceStyle</item>
</style>

<style name="FragmentPreferenceStyle" parent="PreferenceFragment.Material">
    <item name="android:divider">@drawable/list_separator_dark</item>
</style>

list_separator_dark.xml 可绘制资源文件应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:height="1dp" />
    <solid android:color="#212121" />
</shape>