android - 为什么 ListView 在 ListPreference 对话框中停留在 TextView 之上?

标签 android android-listview listpreference

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

@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(使用“下方”或“上方”属性)但没有成功:我找不到将 TextView 放在 ListView 上方的方法...布局非常简单,我看不到为什么列表停留在上面... 另请注意,该问题同时出现在真实设备(Nexus 4、Android 4.2.2)和模拟器上。然而,当查看在 Eclipse 的图形布局中呈现的布局时,布局是正确的!查看两张附加图片。

关于如何解决这个问题有什么想法吗?

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

Layout rendered on the device (incorrect)

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

Layout rendered on Eclipse (correct)

使用解决方案 10.07.2013 编辑

正如已接受的答案所建议的,问题来自于在 ListPreference 的 onPrepareDialogBu​​ilder() 中使用 builder.setSingleChoiceItems()。 我通过扩展 ListPreference 和重写 onCreateDialogView() 来构建没有构建器的对话框来修复它,这样我就可以创建一个自定义 View ,在列表项上方显示标题文本。

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>

最佳答案

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

关于android - 为什么 ListView 在 ListPreference 对话框中停留在 TextView 之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17298547/

相关文章:

android - 如何在 Android 中从 java 代码编辑 ListPreference 值

java - 将 Locale[] 转换为 CharSequence[] 以放置在 Android 应用程序中的 ListPreference 中

android - EditTextPreference 值仅在第二次单击 ListPreference 后刷新

Android:如何使用选择器?

android - 从服务器请求数据的最佳方式

带有 SQL Express 的 Android

android - 设备语言为英文时如何加载arabic string.xml

android - 与 Firefox 和 Chrome 类似的滚动隐藏效果

Android 如何使用简单光标适配器/自定义光标适配器从数据库加载数据并将文本和图像放在一行中

android - listview行自定义布局问题