提示请尽可能阅读有关创建自定义布局列表首选项(背景和布局顶部面板,面板按钮)的信息。 Met - 仅用于自定义行的示例。 对不起 - 谷歌翻译。
答案 0 :(得分:4)
您无法为ListPreference
创建自定义布局。但是,您可以创建自己的自定义DialogPreference
,并将其设置为您想要的任何内容。
例如,here is a DialogPreference
that uses a TimePicker
to allow the user to choose a time。 Here is a DialogPreference
that allows the user to choose a color
答案 1 :(得分:4)
在 preference.xml 文件中,您可以通过类的全名来引用自定义ListPreference,即 com.example.MyPreference
<?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>
然后你的班级 MyPreference 应该是这样的:
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();
}
}
答案 2 :(得分:1)
在您的首选项xml文件
中&LT; your.domain.CustomListPreference ... /&GT;
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);
}
}
}
}