How to change text color of preference category in Android?

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

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




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

    To change text color of preference category only set a theme to your PreferenceActivity in your Android Manifest and make sure that colorAccent item exists. This color is taken by your PreferenceCategory.

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

    One solution is to make a theme for your PreferenceScreen. So in your themes.xml or styles.xml (better to put it in themes.xml) :

    <style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
        <item name="android:textColor">@color/yourCategoryTitleColor</item>
    </style>
    

    then in your AndroidManifest.xml :

    <activity
          android:name="MyPreferenceActivity"
          ...
          android:theme="@style/PreferenceScreen" >
    </activity>
    

    It worked perfectly for me.

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

    A lot of the other answers didn't work for my case. I'm using a PreferenceFragmentCompat and I didn't want to have actual code doing this. So I simply made a copy of the preference category xml file and changed the textColor field. The file goes under the Apache license, Version 2.

    <?xml version="1.0" encoding="utf-8"?>
    
    <!--
      ~ Copyright (C) 2015 The Android Open Source Project
      ~
      ~ Licensed under the Apache License, Version 2.0 (the "License");
      ~ you may not use this file except in compliance with the License.
      ~ You may obtain a copy of the License at
      ~
      ~      http://www.apache.org/licenses/LICENSE-2.0
      ~
      ~ Unless required by applicable law or agreed to in writing, software
      ~ distributed under the License is distributed on an "AS IS" BASIS,
      ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      ~ See the License for the specific language governing permissions and
      ~ limitations under the License -->
    
      <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginStart="?android:attr/listPreferredItemPaddingLeft"
        android:orientation="vertical">
        <TextView
          android:id="@android:id/title"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="16dp"
          android:paddingEnd="?android:attr/listPreferredItemPaddingRight"
          android:textAlignment="viewStart"
          android:textColor="@color/app_accent"
          android:textStyle="bold"
          tools:ignore="RtlSymmetry"/>
        <TextView
          android:id="@android:id/summary"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:ellipsize="end"
          android:singleLine="true"
          android:textColor="?android:attr/textColorSecondary"/>
      </LinearLayout>
    

    And imported it in my .xml layout for the Preference fragment:

     <PreferenceCategory
        android:layout="@layout/preference_category_companion"
        android:key="my_preference_title"
        android:title="@string/my_preference_title">
    
    0 讨论(0)
  • 2020-12-05 04:48
    public class MyPreferenceCategory extends PreferenceCategory {
    
     public MyPreferenceCategory(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
      }
     public MyPreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
     public MyPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
      }
    
     @Override
     protected View onCreateView(ViewGroup parent) {
        // It's just a TextView!
     TextView categoryTitle =  (TextView)super.onCreateView(parent);
     categoryTitle.setTextColor(parent.getResources().getColor(R.color.orange));
    
        return categoryTitle;
      }
    }
    

    And in your prefs.xml:

    <com.your.packagename.MyPreferenceCategory android:title="General">
    .
    .
    .
    </com.your.packagename.MyPreferenceCategory>
    

    Or you can also use this answer

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

    An easy way to do this is to set the custom layout for the preferenceCategory here:

    <PreferenceCategory
        android:layout="@layout/preferences_category"
        android:title="Privacy" >
    

    Then set your code inside your preferences_category layout file:

    <TextView
        android:id="@android:id/title"
        android:textColor="@color/deep_orange_500"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textAllCaps="true"/>
    

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

    Inspired by @AliSh answer, but I needed to only change the colour of one Preference text item. So, for all the Kotlin guys out there:

    class TextColorPreference : Preference {
    
        constructor(context: Context) : super(context)
    
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    
        constructor(
            context: Context, attrs: AttributeSet,
            defStyle: Int
        ) : super(context, attrs, defStyle)
    
        override fun onBindViewHolder(holder: PreferenceViewHolder?) {
            super.onBindViewHolder(holder)
    
            context?.let {
                (holder?.findViewById(android.R.id.title) as? TextView)?.setTextColor(
                    ContextCompat.getColor(
                        it,
                        R.color.colorPrimary
                    )
                )
            }
        }
    }
    

    And then put it in your xml/prefs.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <this.should.be.your.package.TextColorPreference
            android:id="@+id/settings_logout"
            android:key="@string/prefs_key_logout"
            android:title="@string/settings_logout" />
    </PreferenceScreen>
    
    0 讨论(0)
提交回复
热议问题