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

Mic*_*ith 30 android android-preferences

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

<PreferenceCategory
        android:title="Title"
        android:textColor="#00FF00">
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Ali*_*iSh 53

使用此自定义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);
    }
}
Run Code Online (Sandbox Code Playgroud)

并在Pref.xml文件中添加:

<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />
Run Code Online (Sandbox Code Playgroud)

  • 我发现`findViewById()`仍然无法在`onBindView()`中工作.我在`onCreateView()`中做了它,而且工作正常. (3认同)
  • 万分感谢!这是覆盖 `android.support.v7.preference.PreferenceViewHolder` 的代码,替换了 `onBindView` 方法: ```@Override public final void onBindViewHolder(final PreferenceViewHolder vh) { super.onBindViewHolder(vh); } TextView txt = (TextView) vh.findViewById(android.R.id.title); txt.setTextColor(Color.RED); }``` (2认同)

fla*_*yte 35

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

<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
    <item name="android:textColor">@color/yourCategoryTitleColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后在你的AndroidManifest.xml中:

<activity
      android:name="MyPreferenceActivity"
      ...
      android:theme="@style/PreferenceScreen" >
</activity>
Run Code Online (Sandbox Code Playgroud)

它对我来说很完美.


Sim*_*mon 31

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

<PreferenceCategory
    android:layout="@layout/preferences_category"
    android:title="Privacy" >
Run Code Online (Sandbox Code Playgroud)

然后在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"/>
Run Code Online (Sandbox Code Playgroud)

  • 最好的解决方案,因为您可以直接控制 textview 中的所有内容 (2认同)

小智 11

要更改首选项类别的文本颜色,只需PreferenceActivity在Android Manifest中为您设置主题,并确保colorAccent项目存在.这种颜色是由你的PreferenceCategory.

  • 这是正确的答案!将<item name ="colorAccent"> @ android:color/white </ item>添加到PreferenceActivity的主题中,复选框和标题的颜色将更改为白色. (3认同)

小智 9

实际上刚刚发现使用colorAccent.

如果您的应用程序没有使用 样式colorAccent,您可以去styles.xml查找
<item name="colorAccent">@color/colorPrimary</item>,并根据需要更改颜色。

  • IMO,这是棒棒糖和材料设计的正确答案。参考 - http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/frameworks/base/core/res/res/layout/preference_category_material.xml? av=f (2认同)

Bha*_*esh 8

其他方式将在您的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>
Run Code Online (Sandbox Code Playgroud)

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"
    />
Run Code Online (Sandbox Code Playgroud)

如需更多定制,请访问v7 Preferences res

重要提示:我使用PreferenceFragmentCompatLIB V7偏好


avi*_*ney 7

使用 Material Theme,您只需覆盖以下属性:

<style name="YourTheme" parent="@style/Theme.MaterialComponents">
    <item name="colorAccent">@color/your_custom_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)