Tạo kiểu ListPreference
từ XML không trực tiếp thực hiện được. Vấn đề là ListPreference
(thông qua DialogPreference
) gọi số AlertDialog.Builder(Context)
để tạo số Dialog
, thay vì AlertDialog.Builder(Context context, int themeResourceId)
. Trong khi sau này cho phép cung cấp một chủ đề, trước đây không, làm cho nó rơi trở lại một chủ đề Android mặc định. Đối với một dự án, tôi cần một ListPreference
với màu tiêu đề tùy chỉnh, kiểu tùy chỉnh kiểu vô tuyến và bộ chọn ListView tùy chỉnh (về cơ bản, Holo có màu khác). Tôi đã giải quyết vấn đề này bằng cách mở rộng ListPreference
và ghi đè onPrepareDialogBuilder(Builder)
và OnCreateDialogView()
để tôi có thể sử dụng ListView tùy chỉnh với ArrayAdapter đơn giản, thay vì Dialog
được xây dựng trong ListView
(không hỗ trợ tạo kiểu). Tôi cũng phải ghi đè onDialogClosed()
để đặt giá trị phù hợp cho tùy chọn.
Để sử dụng, tất cả những gì bạn phải làm là thay thế tên lớp của tùy chọn trong rom preferences.xml của bạn ListPreference
thành com.your.packagename.ThemedListPreference
. Ngoài ra, việc triển khai thực hiện giống với 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);
}
}
}
}
Đối với các mục ListView tôi đã sử dụng bố cục bên dưới. Lưu ý rằng drawable/btn_radio_holo_light là một XML có thể vẽ như một trong thư mục android-sdk/platforms/android-x/data/res/drawable của bạn, chỉ với các tham chiếu đến các đối tượng vẽ khác nhau.
<?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" />
Đối với bố trí Dialog của tôi (onCreateDialogView()
), tôi sử dụng như sau: Câu trả lời
<?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 tôi đang chờ đợi –