2015-12-18 67 views
0

我正在構建AlertDialog以顯示一個菜單,用戶可以在其中選擇他的手機號碼所屬的國家/地區。修改AlertDialog中ListView的高度

列表中的一個例子是服用點是這樣的:

----------------------------- 
|  PHONE PREFIX  | 
|       | 
| Spain   - 0034 | 
| United Kingdom - 0044 | 
| France   - 0033 | 
|---------------------------| 
| CANCEL   ACCEPT | 
|---------------------------- 

等等...

我創建了一個list_view項目:

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

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     > 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/item_listview_pais" 
     android:textSize="20sp" 
     android:text="United Kingdom" 
     android:layout_alignParentLeft="true" 
     android:textColor="@color/cardview_dark_background" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="10dp" 
     /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/item_listview_guion" 
      android:textSize="20sp" 
      android:text="-" 
      android:layout_centerHorizontal="true" 
      android:textColor="@color/cardview_dark_background" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="10dp" 

      /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/item_listview_prefijo" 
     android:textSize="20sp" 
     android:layout_marginTop="5dp" 
     android:textColor="@color/cardview_dark_background" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="5dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignBaseline="@+id/item_listview_pais" 

     android:text="0034" 

     /> 

    </RelativeLayout> 
</RelativeLayout> 

而且我想要的佈局對於AlertDialog非常簡單:

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

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="30dp" 
       > 
       <ListView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:id="@+id/input_turno_dialog_listview_prefijos"> 

       </ListView> 

      </ScrollView> 
</LinearLayout> 

在我的活動的某一點上,我按下按鈕,並在actionbutton事件內部調用一個PrefijosDS,它給了我一個ArrayList來填充適配器。 Prefijo類是這樣的:

private String Country; 
private String Prefijo; 

所以Prefijo對象可以是這樣的:

Prefijo prefijo = new Prefijo("Spain","0034"); 

與我PrefijoDS我得到一個數組,所以我得到我所需要的數據。然後,我打電話AlertDialog是這樣的:

AlertDialog alertDialog = builder.create(); 
       ListView lv = (ListView) convertView.findViewById(R.id.input_turno_dialog_listview_prefijos); 
       PrefijoDS prefijoDS = new PrefijoDS(getApplicationContext()); 
       ListaPrefijosAdapter prefijosAdapter = new ListaPrefijosAdapter(getApplicationContext()); 
       prefijosAdapter.set((ArrayList<Prefijo>) prefijoDS.obtenerPrefijos()); 
       lv.setAdapter(prefijosAdapter); 

       alertDialog.show(); 

它的工作原理,我得到的AlertDialog顯示,但ListView控件的高度是非常小的,單行項目差不多的高度。我想要的是調整高度,因此它可以在列表中顯示3或4個項目,然後可以滾動其餘項目。我嘗試了不同的方法來製作最高的AlertDialog,但每次獲得相同的尺寸。當我不使用ScrollView時,獲得AlertDialog更大的唯一方法是。然而,這變成了一個非常大的AlertDialog,我不想要。

我試着這樣做:

alertDialog.getWindow().setLayout(200,300); 

但隨後的結果是一個調整AlertDialog但ListView中保持不變。 我該怎麼辦?

非常感謝!

+0

更改ListView的'layout_height'到'match_parent',並刪除了滾動。您也可以擺脫列表項目佈局中的其中一個RelativeLayouts。 –

+0

我做了你所說的。現在我有一個帶有ListView的AlertDialog,但問題是對話框與整個屏幕一樣大。有沒有辦法讓它變得有點狡猾? – Asaak

+0

在LinearLayout上設置精確的高度測量。或者將它的'layout_height'改爲'wrap_content',並在ListView上設置一個精確的高度尺寸。另外,如果你不打算在LinearLayout中添加更多的視圖,你並不需要它。 –

回答

0

更改警報對話框的大小

AlertDialog.Builder adb = new AlertDialog.Builder(this); 
Dialog d = adb.setView(new View(this)).create(); 
// (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.) 

WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
lp.copyFrom(d.getWindow().getAttributes()); 
lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
lp.height = WindowManager.LayoutParams.MATCH_PARENT; 
d.show(); 
d.getWindow().setAttributes(lp); 

還有一件事改變列表視圖layout_height和layout_width到match_parent