Diseñar el ListPreference
desde XML no es posible directamente. El problema es que ListPreference
(a través de DialogPreference
) llama a AlertDialog.Builder(Context)
para compilar su Dialog
, en lugar de AlertDialog.Builder(Context context, int themeResourceId)
. Si bien este último permite proporcionar un tema, el primero no lo hace, lo que hace que retroceda a un tema predeterminado de Android.
Para un proyecto, necesitaba un ListPreference
con un color de título personalizado, un estilo de botón de radio personalizado y un selector de lista de opciones personalizado (básicamente, Holo en un color diferente). Lo resolví extendiendo ListPreference
y anulando onPrepareDialogBuilder(Builder)
y OnCreateDialogView()
para poder usar un ListView personalizado con un ArrayAdapter simple, en lugar de Dialog
's ListView
incorporado (que no admite el estilo). También tuve que anular onDialogClosed()
para establecer el valor correcto para la preferencia.
Para usarlo, todo lo que tiene que hacer es reemplazar el nombre de clase de la preferencia en su preference.xml rom ListPreference
a com.your.packagename.ThemedListPreference
. Aparte de eso, la implementación es idéntica a 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);
}
}
}
}
Para mi lista Ver elementos utilicé el siguiente diseño. Tenga en cuenta que drawable/btn_radio_holo_light es un XML dibujable como el que se encuentra en su carpeta android-sdk/platforms/android-x/data/res/drawable, solo que con referencias a diferentes objetos descartables.
<?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" />
Para mi diseño de diálogo (onCreateDialogView()
), que utilizó la siguiente: Respuesta
<?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 i estoy esperando –