PreferenceFragmentCompat requires preferenceTheme to be set

使用 v7首选项支持库: http://developer.android.com/tools/support-library/features.html#v7-preference中的新 PreferenceFragmentCompat,我得到了这个错误

E  java.lang.IllegalStateException: Must specify preferenceTheme in theme
E      at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:202)

应该设置什么主题?

更新: 我已经尝试使用

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

就像@Bogato 建议的那样,但是它看起来不对劲,甚至在棒棒糖上看起来也很 Holo。

支援图书馆:

enter image description here

原住民偏好:

enter image description here

57769 次浏览

你必须在你的偏好活动的主题中指定偏好主题。

例如:

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

PreferenceThemeOverlay 是偏好 -v7支持库附带的默认主题。

我在自己的风格中加入了“偏好主题”,似乎解决了这个问题:

   <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:actionBarStyle">@style/MyActionBarTheme</item>
<item name="colorPrimary">@color/ColorPrimary</item>
<item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
<item name="preferenceTheme">@style/Preference</item>
</style>

示例项目可以在这里找到

这个错误修复程序可以作为一个等级依赖项使用

< img src = “ https://api.bintray.com/package/gericop/maven/com.takisoft.fix% 3A 偏好-v7/images/download.svg”alt = “ download”>

现在人们可以很容易地使用图书馆。以下是这样做的最快方法,但是您应该查看 README 以获得更多信息。

1. Update your module's gradle file:

compile 'com.takisoft.fix:preference-v7:27.0.0.0'

2. 使用适当的类作为片段的基础

您可以使用 PreferenceFragmentCompatPreferenceFragmentCompatDividers

(导入 PreferenceFragmentCompat时注意适当的包名称!)

3. Use the appropriate theme

将包含 Activity 的主题设置为 @style/PreferenceFixTheme的变体,如 NoActionBar、 Light 等。

要了解更多信息和使用技巧,请访问 项目页面


附言。在本节中,您可以找到导致创建库的详细解决方案,但是我决定删除它,因为它可能会令人困惑。如果您对这些步骤感到好奇,您仍然可以在这个答案的 修订历史中找到它们。

要使用 偏好片段,你必须在你的主题中设置 preferenceTheme:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
...
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

In this way you can customize the preferenceTheme to style the layouts used for each preference type without affecting other parts of your Activity.

让它去吧,使用 API 11 + 。

API 7现在已经快7岁了。

我只是在主题中添加了这一行,它在 API 19及以上版本中运行良好。

<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>

The accepted "Takisoft Fix" seems rather kludgy and after playing around with it, I am still not satisfied with the resulting look.

如果需要 API 9 + PreferenceFragment支持库,我建议使用 物料选择支援图书馆 它完美地工作(至少对我来说)。

我在 API 25中使用的解决方案:

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

然后加上这一行

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

PreferenceThemeOverlay was was already defined, I didn't have to include it. 它可以在我自己的 Android 4. x 和5. x 设备上运行

将这段代码添加到 styles.xml 并根据它进行修改。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>


<style name="PreferenceThemeOverlay">
<item name="preferenceScreenStyle">@style/Preference.PreferenceScreen</item>
<item name="preferenceFragmentStyle">@style/PreferenceFragment</item>
<item name="preferenceCategoryStyle">@style/Preference.Category</item>
<item name="preferenceStyle">@style/Preference</item>
<item name="preferenceInformationStyle">@style/Preference.Information</item>
<item name="checkBoxPreferenceStyle">@style/Preference.CheckBoxPreference</item>
<item name="switchPreferenceCompatStyle">@style/Preference.SwitchPreferenceCompat</item>
<item name="dialogPreferenceStyle">@style/Preference.DialogPreference</item>
<item name="editTextPreferenceStyle">@style/Preference.DialogPreference.EditTextPreference</item>
<item name="preferenceFragmentListStyle">@style/PreferenceFragmentList</item>
</style>

The problem occurs because the AAPT(Android Asset Packaging Tool) cannot find some resources. Fix this issue as follows:

1. 将需要的资源复制到新的响应资源文件夹,如 res _ fw _ v7 _ ferences 或其他名称。

使用 gradle 构建应用程序,并在 build.gradle (模块的)中添加一些额外的 AAPT 选项,如下所示:

android {
...
aaptOptions {
additionalParameters '-S',
'src/main/res_fw_v17_leanback',  // resources in frameworks/support/v17/leanback
'-S',
'src/main/res_fw_v7_preference',  // resources in frameworks/support/v7/preference
'-S',
'src/main/res_fw_v14_preference',  // resources in frameworks/support/v14/preference
'-S',
'src/main/res_fw_v17_preference-leanback',  // resources in frameworks/support/v17/preference-leanback
'-S',
'src/main/res_fw_v7_appcompat',  // resources in frameworks/support/v7/appcompat
'-S',
'src/main/res_fw_v7_recyclerview',  // resources in frameworks/support/v7/recyclerview
'-S',
'src/main/res',  // resources in your application
'--auto-add-overlay',
'--extra-packages',
'android.support.v17.leanback:android.support.v7.preference:android.support.v14.preference:android.support.v17.preference:android.support.v7.appcompat:android.support.v7.recyclerview'
noCompress 'foo', 'bar'
ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~'
}
...
}

希望这对你有帮助:)

看起来 Google 已经解决了这个问题,我已经用 v14-support 版本25.3.1测试过了

1)将 implementation 'com.android.support:preference-v14:25.3.1'加入你的等级。

2)在样式中加入 PreferenceThemeOverlay.v14.Material而不是 PreferenceThemeOverlay

<style name="AppTheme.SettingsTheme" parent="AppTheme.NoActionBar">
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>

3)最后,将样式添加到 Manifest

android:theme="@style/AppTheme.SettingsTheme"

对我来说最有帮助的网站: 中号

作者指出,我们需要添加两个软件包: com.android.support:preference-v7com.android.support:preference-v14,因为第一个软件包是不带样式发布的。

如果成功了告诉我

谢谢@sergio 的回答,使用 <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>而不是 <item name="preferenceTheme">@style/Preference</item>对我来说非常有用。

<style name="IntentTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:actionModeBackground">@color/fulltransparent</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>