请告诉我在哪里可以阅读有关创建自定义布局列表首选项(背景和布局顶部面板,面板按钮)的内容。目前只能找到自定义行的示例。抱歉,以上内容由谷歌翻译器生成。
您不能为ListPreference
创建自定义布局。但是,您可以创建自己的自定义DialogPreference
并设置其外观为您所需的任何外观。
例如,这里有一个使用TimePicker
允许用户选择时间的DialogPreference
。这里有一个允许用户选择颜色的DialogPreference
。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="pref_wifi_key"
android:title="@string/settings">
<ListPreference
android:key="pref_wifi_remove"
android:title="@string/remove_wifi"/>
<com.example.MyPreference
android:title="@string/add_wifi"/>
</PreferenceScreen>
import android.preference.ListPreference;
public class MyPreference extends ListPreference {
Context context;
public MyPreference(Context context, AttributeSet attrs) {
this.context = context;
setDialogLayoutResource(R.layout.yourLayout); //inherited from DialogPreference
setEntries(new CharSequence[] {"one", "two"});
setEntryValues(new CharSequence[] {"item1", "item2"});
}
protected void onDialogClosed(boolean positiveResult) {
Toast.makeText(context, "item_selected",
Toast.LENGTH_SHORT).show();
}
}
在您的偏好设置 xml 文件中
<your.domain.CustomListPreference .../>
CustomListPreference.java
class CustomListPreference extends ListPreference {
mListAdapter = new your_custom_list_adapter();
private int mClickedDialogEntryIndex;
public CustomListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListPreference(Context context) {
super(context);
}
@Override
protected void onPrepareDialogBuilder(Builder builder) {
mClickedDialogEntryIndex = findIndexOfValue(getValue());
builder.setSingleChoiceItems(mListAdapter, mClickedDialogEntryIndex,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (mClickedDialogEntryIndex != which) {
mClickedDialogEntryIndex = which;
CustomListPreference.this.notifyChanged();
}
CustomListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
dialog.dismiss();
}
});
builder.setPositiveButton(null, null);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
CharSequence[] entryValues = getEntryValues();
if (positiveResult && mClickedDialogEntryIndex >= 0 && entryValues != null) {
String value = entryValues[mClickedDialogEntryIndex].toString();
if (callChangeListener(value)) {
setValue(value);
}
}
}
}