2011-06-21 67 views
11

我需要使用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> 

下面是結果:dialog_with_empty_list_view

任何幫助是極大的讚賞。謝謝!

回答

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> 

在佈局