Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中更改首选项类别的文本颜色?_Android_Android Preferences - Fatal编程技术网

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

如何在Android中更改首选项类别的文本颜色?,android,android-preferences,Android,Android Preferences,textColor属性不起作用。以下是我的XML: <PreferenceCategory android:title="Title" android:textColor="#00FF00"> 有什么想法吗?使用此自定义首选项类别: public class MyPreferenceCategory extends PreferenceCategory { public MyPreferenceCategory(Context conte

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

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


有什么想法吗?

使用此自定义首选项类别:

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

一个解决方案是为您的首选项屏幕创建一个主题。 因此,在您的themes.xml或styles.xml中(最好将其放在themes.xml中):


@颜色/yourCategoryTitleColor
然后在AndroidManifest.xml中:

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

它对我非常有效

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>

.
.
.

或者您也可以使用此

一种简单的方法是在此处设置首选项类别的自定义布局:

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

然后在首选项\类别布局文件中设置代码:



另一种方法是在应用程序主题、应用程序级别中提及主题

    <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:首选类别视图

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

如需更多定制,请访问


重要信息:我正在使用PreferenceFragmentCompat from.

更改首选项类别的文本颜色,只需在Android清单中为您的
首选项活动
设置一个主题,并确保colorAccent项存在。此颜色由您的
首选项类别所采用

实际上刚刚使用
颜色重音
发现首选项类别文本

如果你的应用程序没有使用
colorAccent
的样式,你可以转到
style.xml
找到
@color/colorPrimary
,并根据需要更改颜色。

定义你自己的首选主题并覆盖颜色

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

@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@style/AppTheme.PreferenceTheme
`#颜色值`

许多其他答案对我的案例不起作用。我使用的是PreferenceFragmentCompat,我不想让实际的代码来做这件事。因此,我只是复制了一个preferencecategory 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>

并将其导入到my.xml布局中的首选项片段:

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

受@AliSh answer的启发,但我只需要更改一个
首选项
文本项的颜色。所以,对于所有Kotlin的人来说:

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>

使用材质主题,只需覆盖以下属性:

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

@颜色/您的自定义颜色

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

类参考(
上下文:上下文?,
属性集?,
):首选项(上下文、属性){
覆盖BindViewHolder(holder:PreferenceViewHolder?){
super.onBindViewHolder(支架)
holder?.itemView?.findViewById(android.R.id.title)?.setTextColor(颜色.红色)
}
}
然后在
首选项屏幕中正常使用它:

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



我发现
findViewById()
onBindView()中仍然不起作用。我在
onCreateView()
中完成了它,这很有效。
titleView.setTextColor(Color.RED)抛出
NullPointerException
。任何修复?请注意,必须用您自己的MyPreferenceCategory包名替换
ali.UI.Customize
。非常感谢!这是覆盖
android.support.v7.preference.PreferenceViewHolder
的代码,用于替换方法
onBindView
@Override public final void onBindViewHolder(final PreferenceViewHolder vh){super.onBindViewHolder(vh);TextView txt=(TextView)vh.findViewById(android.title);txt.setTextColor(Color.RED);}
这对我来说只需一次修改就非常有效。而不是
categorytle.setTextColor(parent.getResources().getColor(R.color.orange))我必须使用
categorytile.setTextColor(Color.BLACK)这对我不起作用。我得到了一个ClassCastException,因为我的Category类试图被强制转换为一个Preference而不是PreferenceCategory。你知道为什么吗?这是最好的解决方案,因为你可以直接控制文本视图中的所有内容。你是一个救生员!这比在回答类似问题时提供的其他解决方案更简单!谢谢我无法从类别标题到布局文本视图获取文本,它只是一个空白区域。如何添加?最佳解决方案+1这是正确答案!将@android:color/white添加到您的首选项活动的主题中,复选框和标题的颜色将更改为白色。这在android P及其支持库中不再适用。它们任意设置为灰色。我会看看来源,找出一些东西,这是棒棒糖和材料设计的正确答案。参考-谢谢,这实际上是我的错误。这是涵盖所有API级别的最正确答案。仅设置colorAccent对API不起作用,我没有提到这一点,但这可能会起作用,这是我认为最好的方法。@Javi在这里也一样,这个答案允许您动态更改主题详细信息!:)天哪,当你能让答案变得如此简单的时候,所有其他的答案怎么会如此复杂和漫长??非常感谢。它工作得很好,但您需要将这些行添加到样式@color/with@color/with中。这仍然不会更改首选项类别标题(``)的颜色。这非常简单!为什么我要使用自定义类来全局设置类别标题颜色?
<style name="YourTheme" parent="@style/Theme.MaterialComponents">
    <item name="colorAccent">@color/your_custom_color</item>
</style>
<com.github.anastr.myscore.util.pref.DangerPreference
    app:key="deleteServerData"
    app:title="@string/delete_server_data"
    app:iconSpaceReserved="false" />