एक्सएमएल से 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>
plz जवाब मैं –