【问题标题】:How to customize Preference Screen in new AndroidX Library如何在新的 AndroidX 库中自定义首选项屏幕
【发布时间】:2019-06-03 14:05:56
【问题描述】:

我刚刚从支持库迁移到 AndroidX。我的大部分代码工作正常,但突然我的自定义首选项主题停止工作。

我的应用程序大多具有深色背景,因此我将文本颜色设置为白色变体,但在我的设置中,背景颜色为浅色,因此首选项的标题和副标题应为深色变体。在我尝试自定义我的偏好片段的过程中,我使用了来自这个的解决方案 -> How to style PreferenceFragmentCompat 遗憾的是,在从支持库迁移到 AndroidX 之后,这个解决方案停止工作。

这些是我迁移后的实际依赖项:

implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'com.google.android.material:material:1.1.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
implementation 'androidx.preference:preference:1.1.0-alpha02'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha01'
implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

这是我曾经工作的实际主题。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- General theme colors -->
    <item name="colorPrimary">@color/appColorPrimary</item>
    <item name="colorPrimaryDark">@color/appColorPrimaryDark</item>
    <item name="colorAccent">@color/appColorAccent</item>

    <!-- Text Appearance styles -->
    <item name="android:textViewStyle">@style/TextViewStyle.Serif</item>
    <item name="android:textColorPrimary">@color/textColorPrimary</item>
    <item name="android:textColorSecondary">@color/textColorSecondary</item>
    <item name="android:textColorTertiary">@color/textColorTertiary</item>
    <item name="android:textColorLinkInverse">@color/textColorLinkInverse</item>

<!--For the SearchView cursor color-->
    <item name="colorControlActivated">@color/white</item>

<!--Custom styles and themes -->
    <item name="preferenceTheme">@style/AppTheme.PreferenceTheme</item>
    <item name="actionOverflowMenuStyle">@style/AppTheme.OverflowMenu</item>
    <item name="alertDialogTheme">@style/AlertDialogStyle</item>
    <!-- Custom attributes defined in attrs.xml -->
    <item name="dividerColor">@color/dividerColor</item>
</style>


<!--Preference screen theme-->
<style name="AppTheme.PreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
    <!--Overriding textColor primary/secondary from main theme-->
    <item name="android:textColorPrimary">@color/textColorPrimaryInverse</item>
    <item name="android:textColorSecondary">@color/textColorSecondaryInverse</item>
    <item name="android:colorControlActivated">@color/appColorPrimary</item>
</style>

这种新情况有什么解决办法吗?

【问题讨论】:

  • 您的偏好主题parent 现在应该只是PreferenceThemeOverlay,而不是PreferenceThemeOverlay.v14.Material
  • 我已经尝试过这个解决方案,但它仍然使用主主题颜色而不是我的自定义 AppTheme.PreferenceTheme 中提供的颜色:\
  • 您的preference.xml 是否引用了旧版本中的任何内容,例如&lt;android.support.v7.preference.PreferenceCategory&gt;&lt;/&gt;?迁移 AndroidX 后,我将preference.xml 的标签更改为:&lt;PreferenceCategory&gt;&lt;/&gt;。我实现了与您完全一样的自定义首选项主题,并且效果很好。
  • 不,在迁移期间它没有改变,它像这样:
  • 有关于这个话题的消息吗?我这边也有同样的问题。我的应用程序几乎是黑色的,黑色背景上的黑色首选项标题效果不佳。对我有用的唯一解决方案是使用我的自定义文本颜色样式覆盖所有可用的首选项布局。覆盖 textColorPrimary 和 textColorSecondary 的解决方案在迁移到 androidx.preference 后停止工作

标签: android


【解决方案1】:

我在这里添加它,以便将来对某人有所帮助

如果您从 Support Library 迁移到 AndroidX,那么如果您使用 Preference 库的第一个版本,一切都应该是一样的。例如:

implementation 'androidx.preference:preference:1.0.0'

如果您尝试更新到最新版本,您可能会遇到这样的问题。我正在调查这个问题,我找到了原因。 在PreferenceFragmentCompat的最新版本中,我们可以找到这段代码:

// Source code of version 1.1.1
public abstract class PreferenceFragmentCompat extends Fragment implements

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        ...
        getActivity().getTheme().applyStyle(theme, false);
    }
}

我们可以看到,applyStyle 的第二个参数是false。检查该方法的javadoc,我们可以看到:

 * @param resId The resource ID of a style resource from which to
 *              obtain attribute values.
 * @param force If true, values in the style resource will always be
 *              used in the theme; otherwise, they will only be used
 *              if not already defined in the theme.
 */
public void applyStyle(int resId, boolean force)

因此,AppTheme.PreferenceTheme 中定义的属性只有在 AppTheme 本身早期未定义时才会生效。在上面的代码中,我们可以看到两个主题中都定义了一些属性。因此,它们不会在 Preference 屏幕中生效,因为 AppTheme 将被优先考虑。

在第一个版本1.0.0 中,主题以不同的方式应用:

// Source code of version 1.0.0
public abstract class PreferenceFragmentCompat extends Fragment implements

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        ...
        mStyledContext = new ContextThemeWrapper(getActivity(), theme);
        mPreferenceManager = new PreferenceManager(mStyledContext);
    }
}

因此,换句话说:如果您从支持库迁移并且您的偏好屏幕主题损坏,请尽量避免使用可用的最新版本并使用版本1.0.0,例如。

OP在描述中提到他使用的版本:

implementation 'androidx.preference:preference:1.1.0-alpha02'

这个问题阻止我迁移到最新版本,因为我不想为了更新设置屏幕而重新设计整个应用的主题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多