2012-07-23 97 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

相关问题