如何在Android中更改首选项类别的文本颜色?

时间:2022-12-24 21:16:16

The textColor attribute isn't working. Here's my XML:

textColor属性不起作用。这是我的XML:

<PreferenceCategory
        android:title="Title"
        android:textColor="#00FF00">

Any ideas?

8 个解决方案

#1


48  

use this customize PreferenceCategory class :

使用此自定义PreferenceCategory类:

public class MyPreferenceCategory extends PreferenceCategory {
    public MyPreferenceCategory(Context context) {
        super(context);
    }

    public MyPreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyPreferenceCategory(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        TextView titleView = (TextView) view.findViewById(android.R.id.title);
        titleView.setTextColor(Color.RED);
    }
}

and add this at your Pref.xml file :

并在Pref.xml文件中添加:

<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />

#2


28  

One solution is to make a theme for your PreferenceScreen. So in your themes.xml or styles.xml (better to put it in themes.xml) :

一种解决方案是为PreferenceScreen制作主题。所以在你的themes.xml或styles.xml中(最好把它放在themes.xml中):

<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
    <item name="android:textColor">@color/yourCategoryTitleColor</item>
</style>

then in your AndroidManifest.xml :

然后在你的AndroidManifest.xml中:

<activity
      android:name="MyPreferenceActivity"
      ...
      android:theme="@style/PreferenceScreen" >
</activity>

It worked perfectly for me.

它对我来说很完美。

#3


26  

An easy way to do this is to set the custom layout for the preferenceCategory here:

一种简单的方法是在这里设置preferenceCategory的自定义布局:

<PreferenceCategory
    android:layout="@layout/preferences_category"
    android:title="Privacy" >

Then set your code inside your preferences_category layout file:

然后在preferences_category布局文件中设置代码:

<TextView
    android:id="@android:id/title"
    android:textColor="@color/deep_orange_500"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textStyle="bold"
    android:textAllCaps="true"/>

#4


10  

To change text color of preference category only set a theme to your PreferenceActivity in your Android Manifest and make sure that colorAccent item exists. This color is taken by your PreferenceCategory.

要更改首选项类别的文本颜色,只需在Android Manifest中将主题设置为PreferenceActivity,并确保colorAccent项存在。此颜色由您的PreferenceCategory采用。

#5


2  

public class MyPreferenceCategory extends PreferenceCategory {

 public MyPreferenceCategory(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
  }
 public MyPreferenceCategory(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
 public MyPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
  }

 @Override
 protected View onCreateView(ViewGroup parent) {
    // It's just a TextView!
 TextView categoryTitle =  (TextView)super.onCreateView(parent);
 categoryTitle.setTextColor(parent.getResources().getColor(R.color.orange));

    return categoryTitle;
  }
}

And in your prefs.xml:

在你的prefs.xml中:

<com.your.packagename.MyPreferenceCategory android:title="General">
.
.
.
</com.your.packagename.MyPreferenceCategory>

Or you can also use this answer

或者你也可以使用这个答案

#6


2  

Other way around will be mention the theme in your AppTheme, application level

其他方面将提到AppTheme中的主题,应用程序级别

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        .....//your other items
        <item name="preferenceTheme">@style/PrefTheme</item>
    </style>

    <style name="PrefTheme" parent="@style/PreferenceThemeOverlay">
        <item name="preferenceCategoryStyle">@style/CategoryStyle</item>
    </style>

    <style name="CategoryStyle" parent="Preference.Category">
        <item name="android:layout">@layout/pref_category_view</item>
    </style>

XML : pref_category_view

XML:pref_category_view

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@android:id/title"
          style="?android:attr/listSeparatorTextViewStyle"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:textColor="@color/red"
          android:layout_height="wrap_content"
    />

For more customization visit v7 Preferences res

有关更多自定义,请访问v7首选项res

Important: I am using PreferenceFragmentCompat from lib v7 Preference.

重要提示:我正在使用lib v7 Preference中的PreferenceFragmentCompat。

#7


1  

Actually just found out that preference category text using colorAccent.

If your app didn't using style of colorAccent, you can go to styles.xml and find
<item name="colorAccent">@color/colorPrimary</item>, and change the color as you want.

实际上只是使用colorAccent找到了偏好类别文本。如果您的应用没有使用colorAccent样式,您可以转到styles.xml并找到 @ color / colorPrimary ,然后根据需要更改颜色。

#8


1  

Define your own PreferenceTheme and override colors.

定义自己的PreferenceTheme并覆盖颜色。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="preferenceTheme">@style/AppTheme.PreferenceTheme</item>
</style>

<style name="AppTheme.PreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
    <item name="colorAccent">`#color_value`</item>
</style>

#1


48  

use this customize PreferenceCategory class :

使用此自定义PreferenceCategory类:

public class MyPreferenceCategory extends PreferenceCategory {
    public MyPreferenceCategory(Context context) {
        super(context);
    }

    public MyPreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyPreferenceCategory(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        TextView titleView = (TextView) view.findViewById(android.R.id.title);
        titleView.setTextColor(Color.RED);
    }
}

and add this at your Pref.xml file :

并在Pref.xml文件中添加:

<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />

#2


28  

One solution is to make a theme for your PreferenceScreen. So in your themes.xml or styles.xml (better to put it in themes.xml) :

一种解决方案是为PreferenceScreen制作主题。所以在你的themes.xml或styles.xml中(最好把它放在themes.xml中):

<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
    <item name="android:textColor">@color/yourCategoryTitleColor</item>
</style>

then in your AndroidManifest.xml :

然后在你的AndroidManifest.xml中:

<activity
      android:name="MyPreferenceActivity"
      ...
      android:theme="@style/PreferenceScreen" >
</activity>

It worked perfectly for me.

它对我来说很完美。

#3


26  

An easy way to do this is to set the custom layout for the preferenceCategory here:

一种简单的方法是在这里设置preferenceCategory的自定义布局:

<PreferenceCategory
    android:layout="@layout/preferences_category"
    android:title="Privacy" >

Then set your code inside your preferences_category layout file:

然后在preferences_category布局文件中设置代码:

<TextView
    android:id="@android:id/title"
    android:textColor="@color/deep_orange_500"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textStyle="bold"
    android:textAllCaps="true"/>

#4


10  

To change text color of preference category only set a theme to your PreferenceActivity in your Android Manifest and make sure that colorAccent item exists. This color is taken by your PreferenceCategory.

要更改首选项类别的文本颜色,只需在Android Manifest中将主题设置为PreferenceActivity,并确保colorAccent项存在。此颜色由您的PreferenceCategory采用。

#5


2  

public class MyPreferenceCategory extends PreferenceCategory {

 public MyPreferenceCategory(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
  }
 public MyPreferenceCategory(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
 public MyPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
  }

 @Override
 protected View onCreateView(ViewGroup parent) {
    // It's just a TextView!
 TextView categoryTitle =  (TextView)super.onCreateView(parent);
 categoryTitle.setTextColor(parent.getResources().getColor(R.color.orange));

    return categoryTitle;
  }
}

And in your prefs.xml:

在你的prefs.xml中:

<com.your.packagename.MyPreferenceCategory android:title="General">
.
.
.
</com.your.packagename.MyPreferenceCategory>

Or you can also use this answer

或者你也可以使用这个答案

#6


2  

Other way around will be mention the theme in your AppTheme, application level

其他方面将提到AppTheme中的主题,应用程序级别

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        .....//your other items
        <item name="preferenceTheme">@style/PrefTheme</item>
    </style>

    <style name="PrefTheme" parent="@style/PreferenceThemeOverlay">
        <item name="preferenceCategoryStyle">@style/CategoryStyle</item>
    </style>

    <style name="CategoryStyle" parent="Preference.Category">
        <item name="android:layout">@layout/pref_category_view</item>
    </style>

XML : pref_category_view

XML:pref_category_view

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@android:id/title"
          style="?android:attr/listSeparatorTextViewStyle"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:textColor="@color/red"
          android:layout_height="wrap_content"
    />

For more customization visit v7 Preferences res

有关更多自定义,请访问v7首选项res

Important: I am using PreferenceFragmentCompat from lib v7 Preference.

重要提示:我正在使用lib v7 Preference中的PreferenceFragmentCompat。

#7


1  

Actually just found out that preference category text using colorAccent.

If your app didn't using style of colorAccent, you can go to styles.xml and find
<item name="colorAccent">@color/colorPrimary</item>, and change the color as you want.

实际上只是使用colorAccent找到了偏好类别文本。如果您的应用没有使用colorAccent样式,您可以转到styles.xml并找到 @ color / colorPrimary ,然后根据需要更改颜色。

#8


1  

Define your own PreferenceTheme and override colors.

定义自己的PreferenceTheme并覆盖颜色。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="preferenceTheme">@style/AppTheme.PreferenceTheme</item>
</style>

<style name="AppTheme.PreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
    <item name="colorAccent">`#color_value`</item>
</style>