对话框和列表视图的消息对话框

11
我需要创建一个带有ListView和消息的对话框,但是根据http://code.google.com/p/android/issues/detail?id=10948所述,使用标准的AlertDialog不可能实现。因此,我决定创建带有文本和ListView的自定义视图,并将其附加到对话框中。 然而,我的ListView是空白的。这里是Java代码:
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Hello, title!");

    LayoutInflater factory = LayoutInflater.from(this);
    View content = factory.inflate(R.layout.dialog, null);

    ListView lv = (ListView) content.findViewById(R.id.list);
    lv.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_single_choice, ITEMS));
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    builder.setView(content).setPositiveButton("OK", this).setNegativeButton("Cancel", this);

    AlertDialog alert = builder.create();
    alert.show();

我也有:

    final String[] ITEMS = new String[] { "a", "b", "c" };

这是对话框的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello, text!" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
    ></ListView>

</LinearLayout>

这是结果:dialog_with_empty_list_view

非常感谢任何帮助。谢谢!

2个回答

4

您在linearlayout中缺少android:orientation="vertical"

您的xml将会是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello, text!" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"

    ></ListView>

</LinearLayout>

1

设置方向为垂直

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical">

     <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Hello, text!" />

     <ListView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/list"
     ></ListView>

     </LinearLayout>

在你的布局中


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