我需要创建一个自定义ListPreference对话框,以便我可以在List(ListView)上添加一些标题文本(一个TextView)。 扩展ListPreference和覆盖onCreateDialogView()我已经创建MyListPreference类:为什么ListView保持在ListPreference对话框的TextView之上?
@Override
protected View onCreateDialogView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = (View) inflater.inflate(R.layout.dialog_preference_list, null);
return v;
}
我的XML布局dialog_preference_list.xml包含:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbarAlwaysDrawVerticalTrack="true" />
</LinearLayout>
问题:TextView中显示的ListView,而不是下面以上。我需要上面的TextView。我已经尝试了LinearLayout和RelativeLayout(使用“below”或“above”属性),但都没有成功:我无法找到将TextView放在ListView之上的方法...布局非常简单,我看不到为什么列表保持在... 另外,请注意,问题发生在真实设备(Nexus 4,Android 4.2.2)和仿真器上。但是,在查看Eclipse图形布局中呈现的布局时,布局是正确的!看到两个附加的图片。
关于如何解决这个问题的任何想法?
布局呈现的设备(不正确的)上:
布局呈现于Eclipse(正确):
编辑与溶液2013年10月7日
正如接受的答案所示,问题来自于ListPreference的onPrepareDialogBuilder()中builder.setSingleChoiceItems()的使用。 我已经通过扩展ListPreference并覆盖onCreateDialogView()来修复它,以在没有构建器的情况下构建对话框,以便我可以创建自定义视图,以显示列表项上方的标题文本。
GPListPreference.java:
public class GPListPreference extends ListPreference {
...
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
builder.setNegativeButton(null, null);
builder.setPositiveButton(null, null);
}
private int getValueIndex() {
return findIndexOfValue(getValue());
}
@Override
protected View onCreateDialogView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView lv = (ListView) inflater.inflate(R.layout.dialog_preference_list, null);
TextView header = (TextView) inflater.inflate(R.layout.dialog_preference_list_header, null);
header.setText(getDialogMessage()); // you should set the header text as android:dialogMessage in the preference XML
lv.addHeaderView(header);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), R.layout.dialog_preference_list_singlechoice, getEntries());
lv.setAdapter(adapter);
lv.setClickable(true);
lv.setEnabled(true);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setItemChecked(getValueIndex() + 1, true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setValueIndex(position - 1);
getDialog().dismiss();
}
});
return lv;
}
}
dialog_preference_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbarAlwaysDrawVerticalTrack="true" />
dialog_preference_list_singlechoice.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="2dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
dialog_preference_list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
能在您初始化值,你添加代码的名单?或任何引用ListView的代码 – Muzikant
为了清楚起见,你已经试过了:android:layout_above =“@ + id/list”' 和可选的'android:layout_alignParentTop =“true”'原始dialog_preference_list.xml中的TextView, 对不对? – JaKXz
太棒了!谢谢你muslidrikk。 – Tiago