2012-07-23 57 views

Trả lời

44

sử dụng tùy chỉnh lớp PreferenceCategory này:

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

và thêm này vào tập tin Pref.xml của bạn:

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

Tôi thấy rằng 'findViewById()' vẫn không hoạt động trong 'onBindView()'. Tôi đã làm nó trong 'onCreateView()' thay vào đó, mà làm việc. – ryan

+0

'titleView.setTextColor (Color.RED);' ném 'NullPointerException'. Mọi sửa chữa? –

+0

Bản sửa lỗi nằm trong chú thích đầu tiên ... – Rohan

25

Một giải pháp là để thực hiện một chủ đề cho PreferenceScreen của bạn. Vì vậy, trong themes.xml hoặc styles.xml của bạn (tốt hơn để đặt nó trong themes.xml):

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

sau đó trong AndroidManifest.xml của bạn:

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

Nó làm việc hoàn hảo đối với tôi.

+2

Với tôi, đây là câu trả lời đúng – Javi

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

Và trong prefs.xml của bạn:

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

Hoặc bạn cũng có thể sử dụng này answer

+0

Điều này làm việc hoàn hảo cho tôi với một sửa đổi. Thay vì 'categoryTitle.setTextColor (parent.getResources() getColor (R.color.orange).); ' tôi đã phải sử dụng ' categoryTitle.setTextColor (Color.BLACK);' – ckn

25

Một cách dễ dàng để làm điều này là để thiết lập cách bố trí tùy chỉnh cho preferenceCategory đây:

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

Sau đó, đặt mã của bạn bên trong tệp sơ đồ 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

Đây là nhanh chóng và chính xác cách để làm điều đó. –

+0

Giải pháp tốt nhất, bởi vì bạn có quyền kiểm soát trực tiếp mọi thứ bên trong textview – Sniper

+0

bạn là một người tiết kiệm cuộc sống! – CppChase

2

cách khác xung quanh sẽ đề cập đến các chủ đề trong bạn AppTheme, mức độ ứng dụng

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

Để biết thêm tùy lần v7 Preferences res

quan trọng: Tôi đang sử dụng PreferenceFragmentCompat từ lib v7 Preference.

Happy_Coding;

10

Để thay đổi màu sắc văn bản của thể loại chọn này chỉ thiết lập một chủ đề để PreferenceActivity của bạn trong Android Manifest của bạn và chắc chắn rằng colorAccent mục tồn tại. Màu này được chụp bởi PreferenceCategory của bạn.

+2

Đây là câu trả lời đúng! Thêm @ android: color/white vào chủ đề cho PreferenceActivity của bạn và màu của hộp kiểm và tiêu đề sẽ chuyển sang màu trắng. – DiscDev

1

Thực ra chỉ phát hiện ra rằng văn bản danh mục tùy chọn sử dụng colorAccent.

Nếu ứng dụng của bạn không sử dụng kiểu colorAccent, bạn có thể truy cập styles.xml và tìm
<item name="colorAccent">@color/colorPrimary</item> và thay đổi màu như bạn muốn.

+0

IMO, đây là câu trả lời đúng như của Lollipop và thiết kế material design. Tham khảo - 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

Các vấn đề liên quan