2012-05-05 12 views
11

मैंने अपने एप्लिकेशन में सभी रेडियोबटन को अनुकूलित किया है लेकिन सूची में रेडियोबटन को संदर्भित नहीं किया गया है।सूची वरीयता रेडियो बटन को अनुकूलित करने के लिए कैसे करें

मैं इस एक्सएमएल btn_radio.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checked="true" android:state_window_focused="false" 
     android:drawable="@drawable/radio_selected" /> 
<item android:state_checked="false" android:state_window_focused="false" 
     android:drawable="@drawable/radio_unselected" /> 

<item android:state_checked="true" android:state_pressed="true" 
     android:drawable="@drawable/radio_selected" /> 
<item android:state_checked="false" android:state_pressed="true" 
     android:drawable="@drawable/radio_unselected" /> 

<item android:state_checked="true" android:state_focused="true" 
     android:drawable="@drawable/radio_selected" /> 
<item android:state_checked="false" android:state_focused="true" 
     android:drawable="@drawable/radio_unselected" /> 

<item android:state_checked="false" android:drawable="@drawable/radio_unselected" /> 
<item android:state_checked="true" android:drawable="@drawable/radio_selected" /> 
</selector> 

यह customRadioButton जो अपने आवेदन के विषय में एंड्रॉयड कस्टम रेडियो बटन

<style name="CustomRadioButton" Parent="@android:style/Widget.CompoundButton.RadioButton"> 
    <item name="android:button">@drawable/btn_radio</item> 
</style> 

फैली हुई है नामित इस्तेमाल किया है मैं इसे बदल देता

किया है
<item name="android:radioButtonStyle">@style/CustomRadioButton</item> 
    <item name="android:listChoiceIndicatorSingle">@style/CustomRadioButton</item> 

यह अनुकूलन बदलता है ई मेरी सूची में सभी रेडियोबटन मेरे सूची में रेडियोबटन को छोड़कर

+0

plz जवाब मैं –

उत्तर

24

एक्सएमएल से ListPreference स्टाइलिंग सीधे संभव नहीं है। समस्या यह है कि ListPreference (DialogPreference के माध्यम से) के बजाय Dialog बनाने के लिए AlertDialog.Builder(Context) पर कॉल करें। जबकि उत्तरार्द्ध थीम प्रदान करने की अनुमति देता है, पूर्व नहीं करता है, जिससे यह एक डिफ़ॉल्ट एंड्रॉइड थीम पर वापस आ जाता है।

एक प्रोजेक्ट के लिए, मुझे एक कस्टम शीर्षक-रंग, एक कस्टम रेडियोधटन-शैली और कस्टम सूची दृश्य-चयनकर्ता (मूल रूप से, एक अलग रंग में होलो) के साथ ListPreference की आवश्यकता थी। मैंने इसे ListPreference तक बढ़ाकर और onPrepareDialogBuilder(Builder) और OnCreateDialogView() ओवरराइड करके हल किया है, इसलिए मैं Dialog के अंतर्निहित ListView (जिसमें स्टाइल के लिए समर्थन नहीं है) के बजाय एक साधारण ArrayAdapter के साथ एक कस्टम ListView का उपयोग कर सकता है। वरीयता के लिए सही मूल्य निर्धारित करने के लिए मुझे onDialogClosed() को ओवरराइड करना पड़ा।

इसका उपयोग करने के लिए, आपको बस अपनी प्राथमिकताओं में प्राथमिकता के वर्गनाम को प्रतिस्थापित करना है। Xml रोम ListPreference से com.your.packagename.ThemedListPreference। इसके अलावा, कार्यान्वयन ListPreference के समान है।

public class ThemedListPreference extends ListPreference implements OnItemClickListener { 

    public static final String TAG = "ThemedListPreference"; 

    private int mClickedDialogEntryIndex; 

    private CharSequence mDialogTitle; 

    public ThemedListPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ThemedListPreference(Context context) { 
     super(context); 
    } 

    @Override 
    protected View onCreateDialogView() { 
     // inflate custom layout with custom title & listview 
     View view = View.inflate(getContext(), R.layout.dialog_settings_updatetime, null); 

     mDialogTitle = getDialogTitle(); 
     if(mDialogTitle == null) mDialogTitle = getTitle(); 
     ((TextView) view.findViewById(R.id.dialog_title)).setText(mDialogTitle); 

     ListView list = (ListView) view.findViewById(android.R.id.list); 
     // note the layout we're providing for the ListView entries 
     ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
       getContext(), R.layout.btn_radio, 
       getEntries()); 

     list.setAdapter(adapter); 
     list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     list.setItemChecked(findIndexOfValue(getValue()), true); 
     list.setOnItemClickListener(this); 

     return view; 
    } 

    @Override 
    protected void onPrepareDialogBuilder(Builder builder) { 
     // adapted from ListPreference 
     if (getEntries() == null || getEntryValues() == null) { 
      // throws exception 
      super.onPrepareDialogBuilder(builder); 
      return; 
     } 

     mClickedDialogEntryIndex = findIndexOfValue(getValue()); 

     // .setTitle(null) to prevent default (blue) 
     // title+divider from showing up 
     builder.setTitle(null); 

     builder.setPositiveButton(null, null); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 
     mClickedDialogEntryIndex = position; 
     ThemedListPreference.this.onClick(getDialog(), DialogInterface.BUTTON_POSITIVE); 
     getDialog().dismiss(); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
      // adapted from ListPreference 
     super.onDialogClosed(positiveResult); 

     if (positiveResult && mClickedDialogEntryIndex >= 0 
       && getEntryValues() != null) { 
      String value = getEntryValues()[mClickedDialogEntryIndex] 
        .toString(); 
      if (callChangeListener(value)) { 
       setValue(value); 
      } 
     } 
    } 
} 

मेरी सूची दृश्य वस्तुओं के लिए मैंने नीचे दिए गए लेआउट का उपयोग किया। ध्यान दें कि drawable/btn_radio_holo_light एक एक्सएमएल-ड्रायबल है जो आपके एंड्रॉइड-एसडीके/प्लेटफॉर्म/एंड्रॉइड-एक्स/डेटा/रेस/ड्रॉबल फ़ोल्डर में से एक है, केवल विभिन्न ड्रॉबल्स के संदर्भों के साथ।

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/dialog_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:textColor="@color/title_color" 
     android:textSize="22sp" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="2dp" 
     android:background="@color/divider_color" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:listSelector="@drawable/list_selector" /> 

</LinearLayout> 
+0

इंतजार कर रहे हूँ यहाँ CheckedTextView', यह बनाए गए किसी भी कस्टम दृश्य है 'क्या है:

<?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:checkMark="@drawable/btn_radio_holo_light" android:gravity="center_vertical" android:minHeight="@dimen/list_item_minheight" android:paddingLeft="@dimen/list_item_paddingLeft" android:paddingRight="@dimen/list_item_paddingLeft" /> 

मेरी संवाद लेआउट (onCreateDialogView()) के लिए, मैं निम्नलिखित का इस्तेमाल किया। क्या आप कृपया मार्गदर्शन कर सकते हैं। – Dory

+0

इस 'R.layout.btn_radio' लेआउट में क्या होना चाहिए। – Dory

+0

@ निधि गंधिया, [चेकड टेक्स्टव्यू] (http://developer.android.com/reference/android/widget/CheckedTextView.html) एक मानक एंड्रॉइड विजेट है। कोड स्निपेट जो ' Felix

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