2012-07-23 69 views

回答

44

使用這種定製PreferenceCategory類:

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); 
    } 
} 

,並在您的Pref.xml文件補充一點:

<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" /> 
+2

我發現'findViewById()'在'onBindView()'中仍然不起作用。我在'onCreateView()'中做了它,而不是工作。 – ryan 2013-09-20 01:58:08

+0

'titleView.setTextColor(Color.RED);'拋出'NullPointerException'。任何修復? – 2015-11-25 12:49:19

+0

該修復程序本身就是第一個註釋... – Rohan 2016-04-20 09:04:46

25

一種解決方案是創建一個主題爲您PreferenceScreen。 因此,在您的themes.xml或styles.xml(更好地把它的themes.xml):

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

然後在AndroidManifest.xml中:

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

它的工作完美的我。

+2

對我來說,這是正確的答案 – Javi 2017-03-01 11:24:38

2
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; 
    } 
} 

而在你的prefs.xml:

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

或者你也可以使用這個answer

+0

這對我來說是一個完美的修改。 而不是 'categoryTitle.setTextColor(parent.getResources()的getColor(R.color.orange));' 我不得不使用 'categoryTitle.setTextColor(Color.BLACK);' – ckn 2015-11-14 05:54:23

25

一個簡單的方法來做到這一點是在這裏設置了preferenceCategory自定義佈局:

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

然後在您的preferences_category佈局文件中設置您的代碼:

<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

這是快速簡單和正確的方法來做到這一點。 – 2016-07-09 08:32:37

+0

最好的解決方案,因爲你可以直接控制textview中的所有東西 – Sniper 2016-07-26 16:26:10

+0

你是一個拯救生命的人! – CppChase 2016-12-22 11:35:00

2

其他方式將提及的主題在你的AppTheme,應用水平

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

需要更多定製訪問v7 Preferences res

重要:我使用PreferenceFragmentCompatlib v7 Preference

Happy_Coding;

10

要更改偏好類別的文本顏色,請在您的Android清單中將主題設置爲您的PreferenceActivity,並確保colorAccent項存在。這種顏色是由您的PreferenceCategory拍攝的。

+2

這是正確的答案!將 @ android:color/white添加到您的PreferenceActivity主題中,複選框和標題的顏色將更改爲白色。 – DiscDev 2016-09-07 00:23:51

1

其實剛剛發現使用colorAccent的偏好分類文本。

如果您的應用沒有使用colorAccent的樣式,則可以轉到styles.xml並找到
<item name="colorAccent">@color/colorPrimary</item>,然後根據需要更改顏色。

+0

國際海事組織,這是作爲棒棒糖和材料設計的正確答案。參考 - http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/frameworks/base/core/res/res/layout/preference_category_material.xml? AV = F – Ginandi 2017-12-12 21:21:26