【问题标题】:changing the preference category label color background更改偏好类别标签颜色背景
【发布时间】:2011-10-26 06:14:22
【问题描述】:

我想更改偏好类别 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"

但它改变了我所有偏好活动的背景颜色。可以通过哪些方式更改偏好部分的属性?

【问题讨论】:

  • 你能从 logcat 中发布完整的错误日志吗?
  • 我已经编辑了我的问题并发布了完整的错误日志。

标签: android android-preferences


【解决方案1】:

另一个对我有用的解决方案是将 preference_category.xml 复制到 res/layout 文件夹并从那里更改值。

<?xml version="1.0" encoding="utf-8"?>
    <!-- Layout used for PreferenceCategory in a PreferenceActivity. -->
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/listSeparatorTextViewStyle"
        android:textColor="@color/colorPrimaryDark"
        android:id="@android:id/title"
        />

只需在 android:textColor="@color/colorPrimaryDark" 块中提供您想要的任何颜色

【讨论】:

    【解决方案2】:

    对我来说,这是可行的:

    例如 ListSeparator 项:

    在styles.xml中

    <style name="ListSeparator">
    <item name="android:background">@drawable/status_bar</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">25dip</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/black</item>
    <item name="android:textSize">14sp</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:paddingLeft">5sp</item>
    </style>
    
    <color name="custom_theme_color">#e6e6e6</color>
    
    <style name="CustomTheme" parent="android:Theme.Light.NoTitleBar">
    <item name="android:windowBackground">@color/custom_theme_color</item>
    <item name="android:colorBackground">@color/custom_theme_color</item>
    <item name="android:listSeparatorTextViewStyle">@style/ListSeparator</item>
    </style>
    

    然后,在 AndroidManifest.xml 中:

    <activity
    android:name="MyActivityPreference"
    android:theme="@style/CustomTheme" >
    </activity>
    

    或查看此主题: https://stackoverflow.com/a/5564943/471690

    【讨论】:

      【解决方案3】:

      这是我的问题的解决方案。例如在我的文件中,preference_category.xml 我必须有一个简单的 PreferenceCategory 布局,而不是在android:layout 中使用它。但是android:widgetLayout 不起作用。

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@color/my_blue"
      >
      <TextView 
      android:id="@+android:id/title"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:textColor="@color/my_white"
      android:textStyle="bold"
      android:textSize="16sp"
      android:padding="5dp"
      android:layout_marginLeft="10dp"
      />     
      </LinearLayout>
      

      【讨论】:

      • 我走同样的路,发现有些资源是隐藏的,无法设置样式。提供布局的方法有效,而 widgetLayout 将引用列表项中的视图,例如复选框或其他一些小部件。
      【解决方案4】:

      我认为问题在于您试图传递一个样式/主题,该样式/主题中仅指定了背景颜色,而它期望包含一个视图的样式/主题/布局以替换首选项中的当前视图.

      android:widgetLayout 文档中的示例说

      复选框首选项将指定自定义布局(由 只是复选框)在这里

      另一种选择是尝试通过覆盖 PreferenceCategory 的 onBindView() 在代码中执行此操作。

      是的,更改 AndroidManfiest.xml 中的主题应该会根据您指定的位置更改整个应用程序或活动的背景颜色(在这种情况下)。

      【讨论】:

      • 是的,我要写一个简单的布局。使用资源和物品会导致问题。我已经在回答中发布了我的问题的有效解决方案。
      猜你喜欢
      • 2012-05-19
      • 2016-08-20
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多