java - 偏好设置屏幕更改颜色

标签 java android android-studio

我在项目中设置 (PreferenceScreen) 样式时遇到问题。

如何更改(标题)和(摘要)文本的颜色。

尝试了几个小时不同的方法后没有运气,我一定做错了什么

SettingsFragment.Java

public class SettingsFragment extends Fragment {


    public SettingsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getActivity().getFragmentManager().beginTransaction()
                .replace(R.id.flContent, new SettingsPreference())
                .commit();
    }

    public static class SettingsPreference extends PreferenceFragment {

        public SettingsPreference() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.settings);
        }

    }

}

fragment_setings.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/app_background"
    android:clickable="true"
    android:focusable="true"
    tools:context="com.companyv.app.fragments.SettingsFragment">

</FrameLayout>

设置.xml

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

    <PreferenceCategory
        android:key="pref_key_storage_settings"
        android:title="First">

        <CheckBoxPreference
            android:defaultValue="false"
            android:key="pref_key_auto_delete"
            android:summary="Sub 1 summary"
            android:title="Sub 1" />

        <Preference
            android:dependency="pref_key_auto_delete"
            android:key="pref_key_sms_delete_limit"
            android:summary="Sub 2 summary"
            android:title="Sub 2" />

        <Preference
            android:dependency="pref_key_auto_delete"
            android:key="pref_key_mms_delete_limit"
            android:summary="Sub 3 summary"
            android:title="Sub 3" />

    </PreferenceCategory>

</PreferenceScreen>

最佳答案

我之前通过使用自定义首选项主题并设置 自定义主题中的 android:textColor(标题颜色)和 android:textColorSecondary(摘要颜色)。例如,黑色标题和白色摘要:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="preferenceTheme">@style/AppPreferenceTheme</item>
</style>

<!-- Custom Preference Theme -->
<style name="AppPreferenceTheme"
       parent="@style/PreferenceThemeOverlay.v14.Material">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

您可以将“AppPreferenceTheme”重命名为您喜欢的任何名称。

关于java - 偏好设置屏幕更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47897851/

相关文章:

java - 需要找到当前eclipse工作空间中文件的文件路径

android - 离开 Activity 后保留变量

android - 我在 android studio 中实现菜单抽屉时出错

android - 使用 Android Studio 如何获得签名的、非调试的和 zip 对齐的 APK?

java - Android Studio自动生成getters和setters时如何同时生成注释

Android Studio "Project Structure"无法正常运行

java - 请帮助我遵循数据类型

java - Eclipse RCP 中 ContainerSelectionDialog 内的 JFace 选项卡

java - 从插件: "org.eclipse.jface" when using Checkstyle Plugin调用代码时出现问题

android - 应用程序文本大小不随系统字体大小缩放