为什么ListView在ListPreference对话框中保持在TextView之上?

mus*_*ikk 7 android listpreference android-listview

我需要创建一个自定义ListPreference对话框,以便我可以在List(ListView)上方添加一些标题文本(TextView).我创建了MyListPreference类,它扩展了ListPreference并覆盖了onCreateDialogView():

@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;
}
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

问题:TextView显示在ListView下面而不是上面.我需要TextView在上面.我已尝试使用LinearLayout和RelativeLayout(使用"下方"或"上方"属性)但没有成功:我找不到将TextView放在ListView上方的方法...布局非常简单,我看不到为什么列表保持在上面...另外,请注意问题发生在真实设备(Nexus 4,Android 4.2.2)和模拟器上.但是,在查看Eclipse的图形布局中呈现的布局时,布局是正确的!查看附图.

关于如何解决这个问题的任何想法?

在设备上呈现的布局(不正确):

在设备上呈现的布局(不正确)

在Eclipse上呈现的布局(正确):

在Eclipse上呈现的布局(正确)

使用解决方案10.07.2013进行编辑

正如接受的答案所暗示的那样,问题来自在ListPreference的onPrepareDialogBu​​ilder()中使用builder.setSingleChoiceItems().我通过扩展ListPreference并覆盖onCreateDialogView()来修复它,以构建没有构建器的Dialog,这样我就可以创建一个自定义视图,显示列表项上方的标题文本.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Emm*_*uel 4

我认为问题在于 ListPreference 的工作方式。ListPreference 使用 Builder.setSingleChoiceItems() 创建带有 RadioButtons 的行,并且它优先于您尝试添加的自定义布局(在您的情况下是 LinearLayout 内的 TextView 和 ListView。解决方案是扩展 DialogPreference。这里是GitHub 的链接,我在其中创建了一个自定义 DialogPreference 来满足您的需要。我还没有编写 RadioButton 逻辑。