首页 > 解决方案 > 如何更改偏好片段中偏好的文本颜色?

问题描述

我已经阅读了几十个线程,解释了如何在 PreferenceActivity 中更改首选项的文本颜色。我没有偏好活动。我在 res/xml 中使用 PreferenceFragment 和 PreferenceScreen

设置片段

public class SettingsFragment extends PreferenceFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences); // the settings XML here (not the layout)

    }
}

片段设置.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/AppTheme2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

首选项.xml

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

    <PreferenceCategory
        android:title="General">

        <CheckBoxPreference
            android:key="pref_example_checkbox"
            android:title="first box"
            android:summaryOn="This is enabled"
            android:summaryOff="This is disabled"
            android:defaultValue="false" />
    </PreferenceCategory>

    <PreferenceCategory
        android:title="Notifications">

        <SwitchPreference
            android:key="pref_enable_notifications"
            android:title="Notifications on/off"
            android:defaultValue="true" />

        <EditTextPreference
            android:key="pref_notification_time"
            android:title="Notification time"
            android:summary="Some shit about notifications"
            android:inputType="number"
            android:dialogMessage="Dialog message!"
            android:defaultValue="5" />
    </PreferenceCategory>

    <PreferenceCategory
        android:title="About">

        <Preference
            android:key="pref_about_licenses"
            android:title="Licenses" />

        <Preference
            android:key="pref_about_app_version"
            android:title="Version" />
    </PreferenceCategory>

</PreferenceScreen>

我尝试添加android:textColor="@color/white"到 中的所有 Preference 项目preferences.xml,但所有文本仍然是黑色的。所有这些线程都在谈论创建新样式,但没有一个解释如何将其应用于偏好片段。

偏好活动将显示在清单中,可以轻松应用样式。但是对于 a PreferenceFragment,清单中没有条目。此外,似乎必须有一种比创建全新样式更快的方法将文本颜色应用于首选项屏幕。

有些文本是黑色的,有些是橙色的(我的 colorAccent) 这些数据存储在哪里以及如何更改它? 偏好屏幕

标签: javaandroid

解决方案


将此添加到主题

///偏好片段字体颜色

<item name="android:textColorPrimary">@color/white_font_color</item>
<item name="android:textColorSecondary">@color/nice_grey_dark</item>
<item name="android:textColorTertiary">@color/white_font_color</item>

推荐阅读