如何在 Android 中更改偏好类别的文本颜色?

问题描述 投票:0回答:13

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

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

有什么想法吗?

android android-preferences
13个回答
58
投票

使用这个定制的 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);
    }
}

并将其添加到您的 Pref.xml 文件中:

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

50
投票

一个简单的方法是在此处为 preferenceCategory 设置自定义布局:

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

然后在 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"/>


47
投票

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

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

然后在你的 AndroidManifest.xml 中:

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

对我来说效果很好。


14
投票

要更改偏好类别的文本颜色,只需在 Android Manifest 中为您的

PreferenceActivity
设置主题,并确保 colorAccent 项目存在。这个颜色是你拍的
PreferenceCategory
.


13
投票

实际上只是发现使用

colorAccent
的偏好类别文本。

如果你的应用没有使用
colorAccent
的风格,你可以去
styles.xml
找到
<item name="colorAccent">@color/colorPrimary</item>
,并根据需要更改颜色。


8
投票

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

    <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 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"
    />

更多定制访问v7 Preferences res

重要:我正在使用来自lib v7 PreferencePreferenceFragmentCompat


7
投票

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

<style name="YourTheme" parent="@style/Theme.MaterialComponents">
    <item name="colorAccent">@color/your_custom_color</item>
</style>

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;
  }
}

在您的 prefs.xml 中:

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

或者你也可以使用这个answer


2
投票

定义您自己的 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
投票

受@AliSh 回答的启发,但我只需要更改 one

Preference
文本项的颜色。所以,对于所有的科特林人来说:

class TextColorPreference : Preference {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(
        context: Context, attrs: AttributeSet,
        defStyle: Int
    ) : super(context, attrs, defStyle)

    override fun onBindViewHolder(holder: PreferenceViewHolder?) {
        super.onBindViewHolder(holder)

        context?.let {
            (holder?.findViewById(android.R.id.title) as? TextView)?.setTextColor(
                ContextCompat.getColor(
                    it,
                    R.color.colorPrimary
                )
            )
        }
    }
}

然后把它放在你的

xml/prefs.xml
:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <this.should.be.your.package.TextColorPreference
        android:id="@+id/settings_logout"
        android:key="@string/prefs_key_logout"
        android:title="@string/settings_logout" />
</PreferenceScreen>

1
投票

对于 androidx 首选项库,您可以像这样扩展

PreferenceCategory
Preference
类:

class DangerPreference(
    context: Context?,
    attrs: AttributeSet?,
): Preference(context, attrs) {

    override fun onBindViewHolder(holder: PreferenceViewHolder?) {
        super.onBindViewHolder(holder)
        holder?.itemView?.findViewById<TextView>(android.R.id.title)?.setTextColor(Color.RED)
    }
}

然后在你的

PreferenceScreen
中正常使用它:

<com.github.anastr.myscore.util.pref.DangerPreference
    app:key="deleteServerData"
    app:title="@string/delete_server_data"
    app:iconSpaceReserved="false" />

0
投票

很多其他答案对我的情况不起作用。我正在使用 PreferenceFragmentCompat 并且我不想让实际代码执行此操作。所以我简单地复制了首选项类别 xml 文件并更改了 textColor 字段。该文件遵循 Apache 许可,版本 2.

<?xml version="1.0" encoding="utf-8"?>

<!--
  ~ Copyright (C) 2015 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License -->

  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginStart="?android:attr/listPreferredItemPaddingLeft"
    android:orientation="vertical">
    <TextView
      android:id="@android:id/title"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="16dp"
      android:paddingEnd="?android:attr/listPreferredItemPaddingRight"
      android:textAlignment="viewStart"
      android:textColor="@color/app_accent"
      android:textStyle="bold"
      tools:ignore="RtlSymmetry"/>
    <TextView
      android:id="@android:id/summary"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ellipsize="end"
      android:singleLine="true"
      android:textColor="?android:attr/textColorSecondary"/>
  </LinearLayout>

并将其导入到我的 Preference 片段的 .xml 布局中:

 <PreferenceCategory
    android:layout="@layout/preference_category_companion"
    android:key="my_preference_title"
    android:title="@string/my_preference_title">

0
投票

这对我有用,

在您的 AndroidManifest.xml 中,添加以下样式:

    <activity
        android:theme="@style/PreferenceScreen"
        android:name="com.yourapp.id"
        android:label="About"
        android:exported="true">
    </activity>

然后转到您的 styles.xml 并添加以下内容:

<style name="PreferenceScreen" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <item name="android:background">#333333</item>          <!--Change color in Preference background-->
    <item name="android:colorAccent">#FFFFFF</item>         <!--Change color in PreferenceCategory title-->
    <item name="android:textColorPrimary">#FFFFFF</item>    <!--Change color in PreferenceCategory->Preference->app:title -->
    <item name="android:textColorSecondary">#BDBDBD</item>  <!--Change color in PreferenceCategory->Preference->app:summary -->
</style>
© www.soinside.com 2019 - 2024. All rights reserved.