How to change text color of preference category in Android?

前端 未结 11 489
别那么骄傲
别那么骄傲 2020-12-05 04:28

The textColor attribute isn\'t working. Here\'s my XML:




        
相关标签:
11条回答
  • 2020-12-05 04:56

    use this customize PreferenceCategory class :

    public class MyPreferenceCategory extends PreferenceCategory {
        public MyPreferenceCategory(Context context) {
            super(context);
        }
    
        public MyPreferenceCategory(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyPreferenceCategory(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onBindView(View view) {
            super.onBindView(view);
            TextView titleView = (TextView) view.findViewById(android.R.id.title);
            titleView.setTextColor(Color.RED);
        }
    }
    

    and add this at your Pref.xml file :

    <ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />
    
    0 讨论(0)
  • 2020-12-05 04:57

    Using Material Theme, you just have to override the following property :

    <style name="YourTheme" parent="@style/Theme.MaterialComponents">
        <item name="colorAccent">@color/your_custom_color</item>
    </style>
    
    0 讨论(0)
  • 2020-12-05 04:59

    Actually just found out that preference category text using colorAccent.

    If your app didn't using style of colorAccent, you can go to styles.xml and find
    <item name="colorAccent">@color/colorPrimary</item>, and change the color as you want.

    0 讨论(0)
  • 2020-12-05 05:00

    Other way around will be mention the theme in your AppTheme, application level

        <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
            .....//your other items
            <item name="preferenceTheme">@style/PrefTheme</item>
        </style>
    
        <style name="PrefTheme" parent="@style/PreferenceThemeOverlay">
            <item name="preferenceCategoryStyle">@style/CategoryStyle</item>
        </style>
    
        <style name="CategoryStyle" parent="Preference.Category">
            <item name="android:layout">@layout/pref_category_view</item>
        </style>
    

    XML : pref_category_view

    <?xml version="1.0" encoding="utf-8"?>
    <TextView android:id="@android:id/title"
              style="?android:attr/listSeparatorTextViewStyle"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:textColor="@color/red"
              android:layout_height="wrap_content"
        />
    

    For more customization visit v7 Preferences res

    Important: I am using PreferenceFragmentCompat from lib v7 Preference.

    0 讨论(0)
  • 2020-12-05 05:05

    Define your own PreferenceTheme and override colors.

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="preferenceTheme">@style/AppTheme.PreferenceTheme</item>
    </style>
    
    <style name="AppTheme.PreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
        <item name="colorAccent">`#color_value`</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题