請在可以閱讀有關創建自定義佈局列表首選項(背景和佈局頂部面板,面板按鈕)的地方閱讀提示。遇見 - 僅用於自定義行的示例。 對不起 - 谷歌翻譯。自定義佈局ListPreference
2
A
回答
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。
4
在preference.xml文件,你可以通過類,即全名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 HOULD的東西是指您的自定義ListPreference像這樣:
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();
}
}
1
在您的喜好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); } } } }
+0
實際上很簡單。非常感謝。 – Ridcully 2015-07-12 18:58:25
相關問題
- 1. iPhone UITableViewController自定義佈局
- 2. RadioButton的自定義佈局
- 3. ListActivity佈局自定義
- 4. BlackBerry - 自定義佈局
- 5. Android自定義佈局
- 6. Android自定義ListViewItem佈局
- 7. QML自定義佈局
- 8. Java自定義Web佈局
- 9. Android - 自定義AutoCompleteTextView佈局
- 10. ContextMenuStrip自定義佈局
- 11. 自定義Android佈局
- 12. 縮放自定義佈局
- 13. 自定義UICollectionView佈局
- 14. 自定義PopupMenu(佈局)
- 15. 自定義佈局ActionBarSherlock
- 16. Android - 自定義佈局Inflater
- 17. AndroidResideMenu自定義佈局
- 18. 自定義靜態佈局
- 19. 如何從另一個自定義XML佈局引用自定義XML佈局?
- 20. 帶動態條目的自定義ListPreference
- 21. Android自定義ListPreference中的行
- 22. ListPreference - 可以自定義項目嗎?
- 23. 定義佈局
- 24. 自定義佈局XML綁定錯誤
- 25. Android佈局設計特定自定義
- 26. 自定義佈局的自定義鍵盤
- 27. 使用自定義佈局搜索時的自定義建議
- 28. 基於已有佈局的自定義佈局
- 29. 添加自定義相對佈局到一個單排佈局
- 30. Android自定義佈局管理器 - 兒童佈局
我知道這是一個古老的職位,但因爲我我正在編寫自定義的ListPrefence,現在我可能會爲未來的編碼人員留下此評論。 **我認爲無法擴展ListPreference是不好的,因爲它破壞了面向對象的方法**爲了實現我想要的,我實際上覆制了ListPreference代碼的90%。當我開始編寫自定義ListPreference時,我希望只能添加我需要的10%。 – ilomambo 2013-12-27 11:47:24
@ilomambo更像是ListPreference正在被棄用,以支持DialogPreference – tpbapp 2014-03-28 13:23:21