preferenceFragmentCompat需要preferenceTheme要设置 [英] PreferenceFragmentCompat requires preferenceTheme to be set

查看:710
本文介绍了preferenceFragmentCompat需要preferenceTheme要设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着新的preferenceFragmentCompat从V7 preference支持库:<一href="http://developer.android.com/tools/support-library/features.html#v7-$p$pference">http://developer.android.com/tools/support-library/features.html#v7-$p$pference,我得到这个错误

 电子java.lang.IllegalStateException:必须指定preferenceTheme的主题
吃android.support.v7.$p$pference.$p$pferenceFragmentCompat.onCreate($p$pferenceFragmentCompat.java:202)
 

什么主题应该设置?

更新:我用曾尝试

&LT;项目名称=preferenceTheme&GT; @风格/ preferenceThemeOverlay&LT; /项目&GT;

所建议的@Bogato,但它看起来不正确的,看起来非常全息甚至棒棒糖。

支持库:

本机preferences:

解决方案
  

示例项目可以在这里找到

解决方案,以对API 7+同时保持材料主题的API 14+的lib工作( v23.1.0 - 二〇一五年十月二十一日的)

首先,创建一个单独的 styles.xml 的设备7+,另一个用于14+(,也许你可以为21+,等的)。

的V14(及以上)仍然使用V14材料为主题的preference主题( @风格/ preferenceThemeOverlay.v14.Material )的方法如上图所示。

对于V7,我们必须设置preference主题,以原来的:

 &LT;项目名称=preferenceTheme&GT; @风格/ preferenceThemeOverlay&LT; /项目&GT;
 

本办法正常(确实有点物化的)preference主题将在7-13设备使用和材料一个在设备14以上。由于V14的lib需要分SDK设置为14或更高,我们必须使用 Android的工作室推荐破解:我们将覆盖该库的要求。要做到这一点,你必须以下行添加到您的清单:

 &LT;使用-SDK的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    android.support.v14 preference:工具overrideLibrary = /&GT;
 

现在的版本一定会成功。如果您检查7+设备的设计,你可能会看到preference类别的设计看起来非常糟糕。为了解决这个问题,包括在默认以下行(或V7,无论您选择的) styles.xml

 &LT;样式名称=Theme.MyTheme.ListSeparatorTextView&GT;
    &LT;项目名称=机器人:TEXTSIZE&GT; 14sp&LT; /项目&GT;
    &LT;项目名称=机器人:TEXTSTYLE&GT;大胆&LT; /项目&GT;
    &LT;项目名称=机器人:文字颜色&GT; @色/ accent_selector&LT; /项目&GT;
    &LT;项目名称=机器人:paddingTop&GT; 16DP&LT; /项目&GT;
    &LT;项目名称=机器人:layout_marginBottom&GT; 16DP&LT; /项目&GT;
&LT; /风格&GT;
 

当然,你需要在 accent_selector.xml 颜色的目录,使其工作,这是因为的Andr​​oid只是一个解决方法:文字颜色属性必须提供颜色状态列表,而不仅仅是一个单一的颜色:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
    &LT;项目的android:颜色=@颜色/重音/&GT;
&LT; /选择器&GT;
 

然后通过添加以下行它刚才创建的样式应用到你的主题:

 &LT;项目名称=机器人:listSeparatorTextViewStyle&GT; @风格/ Theme.MyTheme.ListSeparatorTextView&LT; /项目&GT;
 

基本上它覆盖了内置的 listSeparatorTextViewStyle ,这是风格的preference类的的TextView ,使它更好看。

就是这样,现在你可以使用API​​的支持LIB 7+不牺牲对设备材质款式上或超过14级。

  

有一些错误,但(?):

     
      
  • 整个preference列表具有左右填充这可以通过有效地覆盖所有preference布局与自定义的包含布局代替列表本身上应用内的边距被除去。      
        
    • 文本大小(尤其是冠军)看起来太大了。为了克服这一点,你可以覆盖安卓textAppearanceLarge 游戏的)和安卓textAppearanceSmall 摘要的),在你的主题文件,但如果你这样做,你可以让你的应用程序的其他部分看起来很糟糕,如此彻底地测试它。
    •   
  •   

更新二零一五年十月一十八日

我测试的支持库的最新版本(23.1.0),它修复了以下错误:

  • 背景选择现在设置
  • 列表对话框的错边的-单选按钮错误(没有提到这一点我自己,因为它是不是真的很烦人,只是对材料指南的)
  • preference标题是不是过大了

正常 @风格/ preferenceThemeOverlay 未兑现呢,所以你还是必须使用V14之一。

谷歌移除更多的EditText上相关的方法,但我的修复仍然有效,虽然有点更新必须向自定义类,所以一定要确保更改为23.1.0,当你更新这些。

更新2015年9月6日

除了昨天发现的错误(和临时修复),这里的另一个问题: preferenceThemeOverlay.v14.Material 没有正确的背景选择。为了解决这个问题,您应该添加以下行到你的主题风格:

 &LT;项目名称=机器人:activatedBackgroundIndicator&GT;安卓?ATTR / selectableItemBackground&LT; /项目&GT;
 

请注意,我并​​没有广泛的所以它可能会弄乱你的应用程序的其他部分进行测试。这只是一个暂时的(即实验)修正错误,直到谷歌发布的lib或任何一个bug更少版本的源$ C ​​$ C,所以我们可以解决这个问题。

更新2015年9月5日

所以,谷歌给了我们,我认为是不理想的,但工程的解决方案。代替使用根据本,

 &LT;项目名称=preferenceTheme&GT; @风格/ preferenceThemeOverlay&LT; /项目&GT;
 

应该使用

 &LT;项目名称=preferenceTheme&GT; @风格/ preferenceThemeOverlay.v14.Material&LT; /项目&GT;
 

这也意味着,即使你只使用V7,你必须包括V14 LIB以及因为说 preferenceThemeOverlay.v14.Material 只可在其中。

另一个bug 是,在低于21 API级别的preferenceCategory元素的文字颜色是不是你在你的风格定义强调色。为了将其设置为你的口音颜色,你必须定义在您的任何资源文件的 preference_fallback_accent_color 颜色值。例如:

 &LT;资源&GT;
    &LT;颜色名称=乡音&GT;#FF4081&LT; /彩色&GT;
    &LT;! - 这是需要为preference_category_material布局使用这种颜色作为文本颜色 - &GT;
    &LT;颜色名称=preference_fallback_accent_color&GT; @颜色/重音和LT; /彩色&GT;
&LT; /资源&GT;
 

而另一个bug 是,preferenceCategory的文字风格的斜体的而不是粗体。为了解决这个问题,你必须重新定义所谓的 preference_TextAppearanceMaterialBody2 风格(这是由以下API级别21 preferenceCategory)的您的任何样式文件:

 &LT;样式名称=preference_TextAppearanceMaterialBody2&GT;
    &LT;项目名称=机器人:TEXTSIZE&GT; 14sp&LT; /项目&GT;
    &LT;项目名称=机器人:fontFamily中&GT;无衬线介质&LT; /项目&GT;
    &LT;项目名称=机器人:TEXTSTYLE&GT;大胆&LT; /项目&GT;
    &LT;项目名称=机器人:文字颜色&GT;安卓?ATTR / textColorPrimary&LT; /项目&GT;
&LT; /风格&GT;
 

更新2015年九月

该问题仍是不会固定在最新的支持库,版本23.0.1,即使更新日志说这样的:

  

有关V7和V14 preference支持库的变化:

     
      
  • 补充材料的设计布局和样式文件。 (问题183376
  •   

更新:临时修正错误,直到谷歌修复它

所以,我放在一起使用自定义样式和布局文件的样本项目。你可以在这里找到它: <一href="https://github.com/Gericop/Android-Support-$p$pference-V7-Fix">https://github.com/Gericop/Android-Support-$p$pference-V7-Fix

报告问题是在这里:<一href="https://$c$c.google.com/p/android/issues/detail?id=183376">https://$c$c.google.com/p/android/issues/detail?id=183376 (好像球员正在努力就可以了)。

测试在API级别21和17,现在这些工作:

  • preferenceCategory
  • preference
  • preferenceCheckbox
  • preferenceSwitchCompat
  • preferenceEditText(虽然我不能改变的对话框的按钮的颜色的)
  • preferenceList(不能改变颜色的)

在行动:

原始的答案

我犯了一个全新的风格是:

 &LT;样式名称=Theme.MyTheme父=@风格/ Theme.AppCompat.NoActionBar&GT;
    &LT;项目名称=preferenceTheme&GT; @风格/ Theme.Wtf preference&LT; /项目&GT;
    &LT;! -  [...]  - &GT;
&LT; /风格&GT;

&LT;样式名称=Theme.Wtf preference父=@风格/ preferenceThemeOverlay&GT;
    &LT;项目名称=preferenceScreenStyle&GT; @风格/ preference preferenceScreen&LT; /项目&GT;
    &LT;项目名称=preferenceFragmentStyle&GT; @风格/ preferenceFragment&LT; /项目&GT;
    &LT;项目名称=preferenceCategoryStyle&GT; @风格/ preference.Category&LT; /项目&GT;
    &LT;项目名称=preferenceStyle&GT; @风格/ preference&LT; /项目&GT;
    &LT;项目名称=preferenceInformationStyle&GT; @风格/ preference.Information&LT; /项目&GT;
    &LT;项目名称=复选框,preferenceStyle&GT; @风格/ preference.CheckBox preference&LT; /项目&GT;
    &LT;项目名称=开关preferenceCompatStyle&GT; @风格/ preference.Switch preferenceCompat&LT; /项目&GT;
    &LT;项目名称=对话框preferenceStyle&GT; @风格/ preference.Dialog preference&LT; /项目&GT;
    &LT;项目name="editText$p$pferenceStyle">@style/$p$pference.Dialog$p$pference.EditText$p$pference</item>
    &LT;项目名称=preferenceFragmentListStyle&GT; @风格/ preferenceFragmentList&LT; /项目&GT;
&LT; /风格&GT;
 

我把它称为跆拳道preference的原因是,如果你复制这到style.xml(或者无论你怎么称呼它),你可以的 CTRL +点击的所有的@款式/ preference [...]的事情,你会看到,这些值可能指的是默认的文件夹中(不能确认为Android的工作室荣获牛逼遵循爆炸.aar文件的下一个环节),因此跆拳道的一部分。我认为谷歌搞砸了事情的风格,他们也还没有公布任何样本/教程,并支持preferenceFragmentCompat页面显示相同的样品作为本机一(除了链接),告诉一无所知的要求 preferenceTheme 的属性,并链接到老设置向导指回本地类,不支持的。

With the new PreferenceFragmentCompat from the v7 Preference Support Library: http://developer.android.com/tools/support-library/features.html#v7-preference, I get this error

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

What theme should be set?

Update: I have tried using

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

as suggested by @Bogato but it doesn't look right and looks very Holo even on Lollipop.

Support library:

Native preferences:

解决方案

The sample project can be found here

Solution to make the lib work on API 7+ while keeping material theme on API 14+ (v23.1.0 - October 21st, 2015)

First of all, create a separate styles.xml for devices 7+ and another one for 14+ (and probably you can create for 21+, etc.).

The v14 (and up) will still use the v14 material themed preference theme (@style/PreferenceThemeOverlay.v14.Material) with the method shown above.

For the v7, we have to set the preference theme to the original one:

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

This way the normal (actually a little materialized) preference theme will be used on devices 7-13 and the material one on devices 14 and up. Since the v14 lib requires the min SDK to be set to 14 or higher, we have to use an Android Studio recommended hack: we will override the library's requirements. To do this, you have to add the following line to your manifest:

<uses-sdk xmlns:tools="http://schemas.android.com/tools"
    tools:overrideLibrary="android.support.v14.preference" />

Now the build will succeed. If you check the design on a 7+ device, you'll probably see that the preference categories' design looks really bad. To fix this, include the following lines in your default (or v7, whichever you chose) styles.xml:

<style name="Theme.MyTheme.ListSeparatorTextView">
    <item name="android:textSize">14sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/accent_selector</item>
    <item name="android:paddingTop">16dp</item>
    <item name="android:layout_marginBottom">16dp</item>
</style>

Of course, you'll need the accent_selector.xml in your color directory to make it work, which is just a workaround since the android:textColor attribute must provide a color state list, not just a single color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/accent" />
</selector>

Then apply the just created style to your main theme by adding the following line to it:

<item name="android:listSeparatorTextViewStyle">@style/Theme.MyTheme.ListSeparatorTextView</item>

Basically it overrides the built-in listSeparatorTextViewStyle, which is the style of the preference category's TextView, to make it better looking.

That's it, now you can use the support lib on API 7+ without sacrificing the material styles on devices on or above level 14.

There are some bugs(?) though:

  • The whole preference list has a left-right padding which could be removed by effectively overriding all the preference layouts with custom ones that contain the padding inside the layouts instead of applying it on the list itself.
    • The text sizes (especially the titles') look too big. To overcome this, you can override the android:textAppearanceLarge (titles) and android:textAppearanceSmall (summaries) in your theme file but if you do so, you might make other parts of your app look bad, so test it thoroughly.


UPDATE October 18th, 2015

I tested the latest version (23.1.0) of the support library and it fixes the following bugs:

  • background selector is now set
  • the list dialog's wrong-side-of-radio-buttons bug (didn't mention this myself as it was not really annoying, just against the material guidelines)
  • preference titles aren't oversized anymore

The normal @style/PreferenceThemeOverlay isn't materialized yet, so you still have to use the v14 one.

Google removed more of the EditText related methods but my fix still works, though a little update had to be made to the custom classes, so make sure you update those when changing to 23.1.0.

UPDATE September 6th, 2015

Besides the bugs (and temporary fixes) discovered yesterday, here's another one: PreferenceThemeOverlay.v14.Material has no correct background selector. To overcome this, you should add the following line to your main theme style:

<item name="android:activatedBackgroundIndicator">?android:attr/selectableItemBackground</item>

Note that I did not test it extensively so it might mess up other parts of your app. This is just a temporary (i.e. experimental) bugfix until Google releases either a less buggy version of the lib or the source code so we could fix it.

UPDATE September 5th, 2015

So, Google gives us a solution which I think is not ideal but works. According to this, instead of using

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

one should use

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

This also means that even though you are using only v7, you have to include the v14 lib as well because the said PreferenceThemeOverlay.v14.Material is only available in it.

Another bug is that on API levels below 21 the PreferenceCategory elements' text color is not the accent color you define in your style. To set it to your accent color, you have to define a preference_fallback_accent_color color value in any of your resources files. Example:

<resources>
    <color name="accent">#FF4081</color>
    <!-- this is needed as preference_category_material layout is using this color as the text color -->
    <color name="preference_fallback_accent_color">@color/accent</color>
</resources>

And another bug is that the PreferenceCategory's text style is italic instead of bold. In order to fix this, you have to re-define a so-called Preference_TextAppearanceMaterialBody2 style (this is used by the PreferenceCategory below API level 21) in any of your styles file:

<style name="Preference_TextAppearanceMaterialBody2">
    <item name="android:textSize">14sp</item>
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">?android:attr/textColorPrimary</item>
</style>

UPDATE September 2015

The issue is still NOT fixed in the latest support library, revision 23.0.1, even though the changelog says this:

Changes for v7 and v14 Preference Support library:

  • Added the material design layout and style files. (Issue 183376)

UPDATE: temporary bugfix until Google fixes it

So, I put together a sample project with the customized style and layout files. You can find it here: https://github.com/Gericop/Android-Support-Preference-V7-Fix

The reported issue is here: https://code.google.com/p/android/issues/detail?id=183376 (seems like the guys are working on it).

Tested on API level 21 and 17. These work now:

  • PreferenceCategory
  • Preference
  • PreferenceCheckbox
  • PreferenceSwitchCompat
  • PreferenceEditText (though I couldn't change the color of the dialog's buttons)
  • PreferenceList (cannot change the color)

In action:

Original answer

I made a whole new style for it:

<style name="Theme.MyTheme" parent="@style/Theme.AppCompat.NoActionBar">
    <item name="preferenceTheme">@style/Theme.WtfPreference</item>
    <!-- [...] -->
</style>

<style name="Theme.WtfPreference" parent="@style/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 reason I call it "WtfPreference" is that if you copy this into your style.xml (or whatever you call it), you can CTRL + click on all the @style/Preference[...] things and you'll see that those values probably refer to the default values folder (cannot confirm as the Android Studio won't follow the next links in the exploded .aar file), hence the Wtf part. I think Google messed something up with the styles, they also haven't released any sample / tutorial for it and the support PreferenceFragmentCompat page shows the same sample as the native one (except for the links), telling nothing about the required preferenceTheme attribute and linking to the 'old' settings guide which refers back to the native classes, not the support ones.

这篇关于preferenceFragmentCompat需要preferenceTheme要设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆