gpt4 book ai didi

android - 如何在新的 AndroidX 库中自定义首选项屏幕

转载 作者:行者123 更新时间:2023-12-04 04:27:55 24 4
gpt4 key购买 nike

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

我的应用程序大多具有深色背景,因此我将文本颜色设置为白色变体,但在我的设置中,背景颜色为浅色,因此首选项的标题和副标题应为深色变体。在我尝试自定义我的偏好 fragment 时,我使用了这个解决方案 -> 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>

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

最佳答案

我在此处添加此内容,以便将来可能对某人有所帮助
如果您从 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'
这个问题阻止我迁移到最新版本,因为我不想为了更新设置屏幕而重新设计整个应用程序的主题。

关于android - 如何在新的 AndroidX 库中自定义首选项屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54099284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com