PreferenceScreen更改颜色

时间:2017-12-20 02:12:34

标签: 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>

的settings.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>

1 个答案:

答案 0 :(得分:0)

我之前使用自定义首选项主题并设置了 自定义主题中的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>

您可以重命名&#34; AppPreferenceTheme&#34;无论你喜欢什么名字。