更改首选项类别标签颜色背景
我想更改首选项类别 bakcgorund 的颜色。在带有首选项的 xml 中,我以这种方式键入类别:
<PreferenceCategory android:title="Synchronization"> ... </PreferenceCategory>
我正在尝试使用 xml 属性更改背景:
android:widgetLayout="@style/PreferenceCategory"
我的 styles.xml 中的代码如下所示:
<resources>
<style name="PreferenceCategory">
<item name="android:background">@color/my_blue</item>
</style>
</resources>
但它无法正常工作。我有致命异常:
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)
当我将颜色形式 @color/my_blue
更改为某些十六进制代码时,我也有同样的致命异常。
我知道我可以在 AndroidManifest 中为我的首选项活动使用此 xml 属性:
android:theme="@style/PreferencesTheme"
但它会更改我所有首选项活动的背景颜色。我可以通过什么方式更改首选项部分的属性?
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>
I am trying to change the background using xml attribute:
android:widgetLayout="@style/PreferenceCategory"
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)
I have the same fatal exception when I change the color form @color/my_blue
to some hex code.
I know that I can use in AndroidManifest for my preference activity this xml attribute:
android:theme="@style/PreferencesTheme"
But it changes the background color of all my preference activity. In which way can I change the attributes of preferences parts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说,这有效:
例如 ListSeparator 项:
在 styles.xml 中
然后,在 AndroidManifest.xml 中:
或查看此线程:
https://stackoverflow.com/a/5564943/471690
To me, this works:
For example ListSeparator item:
In styles.xml
Then, in AndroidManifest.xml:
Or view this thread:
https://stackoverflow.com/a/5564943/471690
这是我的问题的解决方案。在我的文件中,例如preference_category.xml,我必须有一个简单的PreferenceCategory 布局,然后在
android:layout
中使用它。但是android:widgetLayout
不起作用。This is solution for my problem. In my file for example preference_category.xml I have to have a simple layout for the PreferenceCategory and than use it in
android:layout
. Howeverandroid:widgetLayout
doesn't work.我认为问题在于,当您期望包含一个视图来替换首选项中的当前视图的样式/主题/布局时,您试图传递一个仅指定背景颜色的样式/主题。
android:widgetLayout 文档中的示例说
另一个选择是尝试通过重写 PreferenceCategory 的 onBindView() 在代码中执行此操作
是的,更改 AndroidManfiest.xml 中的主题应该更改整个应用程序或活动的背景颜色(在本例中),具体取决于您指定的位置。
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.
The example in the documentation for android:widgetLayout says
Another option would be to try and do this in code by overriding the onBindView() of the PreferenceCategory.
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.
对我有用的另一个解决方案是将首选项_category.xml 复制到 res/layout 文件夹中并从那里更改值。
只需在
android:textColor="@color/colorPrimaryDark"
块中指定您想要的颜色即可Another solution that worked for me is just to copy the preference_category.xml into res/layout folder and change the values right from there.
Just give the what ever color you want at the
android:textColor="@color/colorPrimaryDark"
block