PreferenceScreen更改颜色

问题描述 投票:1回答:1

我在项目中设置了(PreferenceScreen)的问题。

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

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

settings fragment.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);
        }

    }

}

Fregment_setingskkml

<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>
java android android-studio
1个回答
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>

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

© www.soinside.com 2019 - 2024. All rights reserved.