本文介绍了如何更改首选项类别中的标题文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<PreferenceCategory android:title="Login email">
    <PreferenceScreen
        android:key="display_email"
        android:title="test email"/>
</PreferenceCategory>

<PreferenceCategory android:title="@string/server_settings_title">
    <PreferenceScreen
        android:key="Server_Select"
        android:summary="@string/server_select_summary"
        android:title="@string/server_pref"/>
</PreferenceCategory>

<PreferenceCategory android:title="@string/settings_title">
    <SwitchPreference
        android:id="@+id/enter_send"
        android:defaultValue="true"
        android:key="@string/settings_enterPreference_key"
        android:summary="@string/settings_enterPreference_summary"
        android:title="@string/settings_enterPreference_label" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/settings_mic">
    <SwitchPreference
        android:id="@+id/mic_input"
        android:defaultValue="true"
        android:key="@string/setting_mic_key"
        android:summary="@string/settings_micPreference_summary"
        android:title="@string/setting_mic_input" />

    <SwitchPreference
        android:id="@+id/hotword_detection"
        android:defaultValue="false"
        android:key="@string/setting_hotword_key"
        android:summary="@string/settings_hotwordPreference_summary"
        android:title="@string/setting_hotword_detection" />
</PreferenceCategory>

我想更改类别而不是屏幕的文本颜色.我已经提到了许多与此有关的文档和博客文章,但都是徒劳的.这是大型代码库的一部分,因此我要小心

I want to change the text-color of the Category and not screen. I have referred to many docs and blog posts regarding this but in vain. This is part of a large code base and hence I want to be careful

推荐答案

创建一个扩展PreferenceCategory的类.

Create a class which extends PreferenceCategory.

public class CustomPreferenceCategory extends PreferenceCategory {

@TargetApi(21)
public CustomPreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    this.init(context, attrs);
}

public CustomPreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.init(context, attrs);
}

public CustomPreferenceCategory(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.init(context, attrs);
}

public CustomPreferenceCategory(Context context) {
    super(context);
    this.init(context, (AttributeSet)null);
}

public void onBindViewHolder(PreferenceViewHolder holder) {
    super.onBindViewHolder(holder);
    TextView title = (TextView)holder.itemView;
    title.setTextColor(Color.BLACK);
}
private void init(Context context, AttributeSet attrs) {
    this.setLayoutResource(R.layout.preference_category);
}

然后将PreferenceCategory替换为xml中的自定义类的名称,如下所示:

Then replace PreferenceCategory with name of custom class in xml like this:

<mypackage.CustomPreferenceCategory android:title="Login email">

这篇关于如何更改首选项类别中的标题文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:08