为什么在ListPreference对话框中ListView会位于TextView之上?

7

我需要创建一个自定义的ListPreference对话框,以便我可以在列表(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;
}

我的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
只是为了明确,你已经尝试在原始的dialog_preference_list.xml中的TextView中使用 android:layout_above="@+id/list" 和可选的 android:layout_alignParentTop="true",对吗? - JaKXz
很棒的答案!谢谢muslidrikk。 - Bitcoin Cash - ADA enthusiast
4个回答

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

感谢您的建议。确实,问题来自于Builder.setSingleChoiceItems()。我通过扩展ListPreference并覆盖onCreateDialogView()来解决了这个问题,以不使用对话框构建器。我会将代码放在单独的答案中。 - gpo
很高兴我能帮到你。GitHub链接有帮助吗? - Emmanuel

2
我猜这是一个主题问题。尝试在构造函数中更改对话框的主题,例如 setStyle(STYLE_NO_TITLE, R.style.AppTheme)。使用无标题样式的基本应用程序主题。
如果这不是问题,那么可能与 ListPreference 类本身有关。它可能会覆盖您的布局以保持一致的主题偏好视图。但是,我以前没有使用过 ListPreference,所以这只是一个猜测。
您能否通过在 XML 图形化布局预览中玩弄主题来重现相同的结果?

谢谢您的建议。然而,在尝试了不同的主题后,发现这并不是问题所在。 - gpo

1
另一个选项是将 TextView 添加为 ListView 的标题,方法如下:
TextView textView = new TextView(getActivity());
ListView listView = new ListView(getActivity());
listView.addHeaderView(textView);

“addHeaderView”需要一个“View”,因此理论上您可以使用任何想要的内容作为标题,但我只使用过“TextView”。

谢谢,但是在ListPreference的ListView上使用addHeaderView()似乎不起作用... - gpo

0
上面的链接已经失效了。在这个解决方案中,想法是覆盖ListPreference,并使用在ListPreference中定义的数据填充自己的ListView。
@Override
protected View onCreateDialogView() {

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ListView lv = new ListView(getContext());

    // Inflate the view into the header only if a message was set
    if (getDialogMessage() != null && ! getDialogMessage().equals("") ) {

        TextView header = (TextView) inflater.inflate(R.layout.dialog_preference_list_header, null);
        header.setText(getDialogMessage());
        lv.addHeaderView(header, null, false);

    }

    // Create a new adapter and a list view and feed it with the ListPreference entries
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(),
            R.layout.custom_dialog_single_choice_list_adapter, 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;
}

另一个重要的事情是调用onPrepareDialogBuilder而不在其中调用super。这将避免列表视图出现两次。
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    // Not calling super, to avoid having 2 listviews

    // Set the positive button as null
    builder.setPositiveButton(null, null);
}

private int getValueIndex() {
    return findIndexOfValue(getValue());
}

在我的情况下,dialog_preference_list_header 只是一个 TestView,但它也可以是一个更复杂的视图,而 custom_dialog_single_choice_list_adapter 则可能是这样的:

<?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" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接