【问题标题】:Dialog with list view and message带有列表视图和消息的对话框
【发布时间】:2011-09-19 09:41:48
【问题描述】:

我需要使用 ListView 和消息创建对话框,但是根据http://code.google.com/p/android/issues/detail?id=10948,标准的 AlertDialog 是不可能的。所以我决定用文本和列表视图创建自定义视图,并将其附加到对话框。

但是,我的列表视图是空的。这是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>

这是结果:

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

【问题讨论】:

    标签: android listview dialog android-alertdialog


    【解决方案1】:

    您在线性布局中缺少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>
    

    【讨论】:

      【解决方案2】:

      设置方向是垂直的

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

      在你的布局中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-29
        • 1970-01-01
        • 2016-06-19
        • 2016-11-13
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多