【发布时间】: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 是否引用了旧版本中的任何内容,例如
<android.support.v7.preference.PreferenceCategory></>?迁移 AndroidX 后,我将preference.xml 的标签更改为:<PreferenceCategory></>。我实现了与您完全一样的自定义首选项主题,并且效果很好。 -
不,在迁移期间它没有改变,它像这样:
-
有关于这个话题的消息吗?我这边也有同样的问题。我的应用程序几乎是黑色的,黑色背景上的黑色首选项标题效果不佳。对我有用的唯一解决方案是使用我的自定义文本颜色样式覆盖所有可用的首选项布局。覆盖 textColorPrimary 和 textColorSecondary 的解决方案在迁移到 androidx.preference 后停止工作
标签: android