Android 更改首选项类别标签颜色背景

Android 更改首选项类别标签颜色背景,android,android-preferences,Android,Android Preferences,我想更改首选类别bakcgorund的颜色。在带有首选项的xml中,我以以下方式键入了类别: <PreferenceCategory android:title="Synchronization"> ... </PreferenceCategory> my styles.xml中的代码如下所示: <resources> <style name="PreferenceCategory"> <item name="android:bac

我想更改首选类别bakcgorund的颜色。在带有首选项的xml中,我以以下方式键入了类别:

<PreferenceCategory android:title="Synchronization"> ... </PreferenceCategory>
my styles.xml中的代码如下所示:

<resources>
<style name="PreferenceCategory">
    <item name="android:background">@color/my_blue</item>
</style>
</resources>
当我将颜色形式
@color/my_blue
更改为某个十六进制代码时,我有同样的致命异常

我知道我可以在AndroidManifest中为我的首选项活动使用此xml属性:

android:widgetLayout="@style/PreferenceCategory"
android:theme="@style/PreferencesTheme"

但它改变了我所有偏好活动的背景色。我可以用哪种方式更改首选项部分的属性?

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

android文档中的示例:widgetLayout说

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

另一个选项是通过重写PreferenceCategory的onBindView()在代码中尝试这样做


是的,更改AndroidManfiest.xml中的主题应该会更改整个应用程序或活动的背景色(在本例中),具体取决于您指定的位置。

这是我问题的解决方案。例如,在我的文件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>

对我来说,这很有效:

例如,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>

@可拉伸/状态栏
匹配父项
25度
大胆的
@颜色/黑色
14便士
中心垂直
5便士
#E6E6
@颜色/自定义主题颜色
@颜色/自定义主题颜色
@样式/列表分隔符
然后,在AndroidManifest.xml中:

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

另一个对我有效的解决方案是将preference\u 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”
块中给出您想要的颜色即可

您可以为此从logcat发布完整的错误日志吗?我已经编辑了我的问题并发布了完整的错误日志。是的,我必须编写一个简单的布局。使用资源和项目会导致问题。我已经发布了我的问题的工作解决方案作为答案。我走了同样的路,发现一些资源是隐藏的,无法设置样式。提供布局的方法有效,而widgetLayout将引用列表项中的视图,如复选框或其他小部件。
<?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"
        />