2012-07-23 10 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()' अभी भी 'bindView() 'में काम नहीं किया है। मैंने इसे 'क्रिएटिव व्यू()' में किया, जिसने काम किया। – ryan

+0

'titleView.setTextColor (Color.RED);' 'NullPointerException 'फेंकता है। कोई फिक्स? –

+0

फिक्स पहली टिप्पणी में ही है ... – Rohan

25

एक समाधान अपने PreferenceScreen के लिए एक थीम बनाना है। अपने AndroidManifest.xml में

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

तो: तो अपने themes.xml या styles.xml में (बेहतर यह themes.xml में डालने के लिए)

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

यह मेरे लिए पूरी तरह से काम किया।

+2

मेरे लिए, यह सही जवाब है – 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; 
    } 
} 

और अपने 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

25

यह करने के लिए एक आसान तरीका है का उपयोग कर सकते यहाँ preferenceCategory के लिए कस्टम लेआउट सेट करने के लिए है:

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

फिर अपना कोड अपनी प्राथमिकताओं_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

यह वह जगह है इसे करने के लिए त्वरित आसान और सही तरीका। –

+0

सबसे अच्छा समाधान, क्योंकि आपके पास टेक्स्टव्यू – Sniper

+0

के अंदर सब कुछ का प्रत्यक्ष नियंत्रण है, आप एक जीवन बचतकर्ता हैं! – CppChase

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> 

एक्सएमएल: 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

महत्वपूर्ण के लिए: मैं lib v7 Preference से PreferenceFragmentCompat उपयोग कर रहा हूँ।

हैप्पी_Coding;

10

वरीयता श्रेणी के टेक्स्ट रंग को बदलने के लिए केवल अपने एंड्रॉइड मैनिफेस्ट में अपने PreferenceActivity पर एक थीम सेट करें और सुनिश्चित करें कि colorAccent आइटम मौजूद है। यह रंग आपके PreferenceCategory द्वारा लिया जाता है।

+2

यह सही जवाब है! @ android जोड़ें: रंग/सफेद अपनी प्राथमिकता के लिए थीम पर और चेकबॉक्स और शीर्षक का रंग सफेद में बदल जाएगा। – DiscDev

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 = च – Ginandi

संबंधित मुद्दे