问题描述
我想改变preference类bakcgorund的颜色。在我的XML与preferences我有这样类型的类:
I would like to change the color of the preference category bakcgorund. In my xml with preferences I have categories typed in this way:
<PreferenceCategory android:title="Synchronization"> ... </PreferenceCategory>
我试图用XML属性来更改背景:
I am trying to change the background using xml attribute:
android:widgetLayout="@style/PreferenceCategory"
在我styles.xml的code如下:
And the code in my styles.xml looks like:
<resources>
<style name="PreferenceCategory">
<item name="android:background">@color/my_blue</item>
</style>
</resources>
但它无法正常工作。我有致命异常:
But it isn't work properly. I have fatal exception:
08-13 01:43:09.111: WARN/dalvikvm(1329): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): FATAL EXCEPTION: main
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): android.content.res.Resources$NotFoundException: Resource ID #0x7f090006
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.content.res.Resources.getValue(Resources.java:892)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.content.res.Resources.getLayout(Resources.java:731)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.preference.Preference.onCreateView(Preference.java:416)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.preference.Preference.getView(Preference.java:389)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.widget.AbsListView.obtainView(AbsListView.java:1418)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.widget.ListView.makeAndAddView(ListView.java:1745)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.widget.ListView.fillDown(ListView.java:670)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.widget.ListView.fillFromTop(ListView.java:727)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.widget.ListView.layoutChildren(ListView.java:1598)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.widget.AbsListView.onLayout(AbsListView.java:1248)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.view.View.layout(View.java:7175)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.view.View.layout(View.java:7175)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.view.View.layout(View.java:7175)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.view.View.layout(View.java:7175)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.os.Looper.loop(Looper.java:123)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at android.app.ActivityThread.main(ActivityThread.java:3647)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at java.lang.reflect.Method.invoke(Method.java:507)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): at dalvik.system.NativeStart.main(Native Method)
我有同样的致命异常,当我改变颜色的形式 @色/ my_blue
来一些十六进制code。
I have the same fatal exception when I change the color form @color/my_blue
to some hex code.
我知道我可以在AndroidManifest用我的preference活动这个XML属性:
I know that I can use in AndroidManifest for my preference activity this xml attribute:
android:theme="@style/PreferencesTheme"
但它改变了我所有的preference活动的背景色。以何种方式我可以改变的preferences部件的属性?
But it changes the background color of all my preference activity. In which way can I change the attributes of preferences parts?
推荐答案
我认为问题是,你试图通过一个款式/的主题,只有当它被期待的风格/主题,它指定了背景色/包含视图布局,以取代与preference当前视图。
I think the problem is that you are trying to pass a style/theme that only has a background color specified in it when it is expecting a style/theme/layout that contains a view to replace the current view in the preference with.
针对Android的文档中的示例:widgetLayout说:
The example in the documentation for android:widgetLayout says
一个复选框,preference将指定一个自定义布局(包括
只是复选框),此处
另一种选择是由preferenceCategory的覆盖onBindView()尝试在code做到这一点。
Another option would be to try and do this in code by overriding the onBindView() of the PreferenceCategory.
和肯定,在AndroidManfiest.xml更改主题应视,你指定它改变了整个应用程序或活动的背景颜色(在这种情况下)。
and yes, changing the theme in the AndroidManfiest.xml should change the background color (in this case) for the entire Application or Activity depending on where you specified it.
这篇关于改变preference类别标签背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!