2011-06-21 20 views
11

Tôi cần tạo hộp thoại có cả ListView và tin nhắn, tuy nhiên theo http://code.google.com/p/android/issues/detail?id=10948 thì không thể với AlertDialog chuẩn. Vì vậy, tôi đã quyết định tạo chế độ xem tùy chỉnh với văn bản và danh sách xem và đính kèm nó vào hộp thoại.Hộp thoại với chế độ xem danh sách và thông báo

Tuy nhiên, chế độ xem danh sách của tôi bị trống. Đây là mã 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(); 

Ngoài ra tôi có:

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

và đây là cách bố trí thoại:

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

Dưới đây là kết quả: dialog_with_empty_list_view

Any help is appreciated rất nhiều . Cảm ơn!

Trả lời

4

Bạn đang thiếu android:orientation="vertical" trong linearlayout.

xml của bạn sẽ

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

bộ định hướng được thẳng đứng như

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

trong cách bố trí của bạn

Các vấn đề liên quan