简体   繁体   English

首选项屏幕更改颜色

[英]PreferenceScreen change colors

I have an issue styling the (PreferenceScreen) in my project. 我在项目中的(PreferenceScreen)样式存在问题。

How can I change the colors of of ( Title ) & ( Summary ) texts. 如何更改( 标题 )和( 摘要 )文本的颜色。

No luck after trying for hours with different methods, I must be doing something wrong 用不同的方法尝试了几个小时后没有运气,我一定做错了

SettingsFragment.Java 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 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 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>

I have done this before by using a custom preference theme and setting the android:textColor (title color) and android:textColorSecondary (summary color) in the custom theme. 我之前通过使用自定义首选项主题并在自定义主题中设置了android:textColor (标题颜色)和android:textColorSecondary (摘要颜色)来完成此操作。 For example, black title and white summary: 例如,黑色标题和白色摘要:

<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>

You can rename "AppPreferenceTheme" to whatever name you like. 您可以将“ AppPreferenceTheme”重命名为您喜欢的任何名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM