我正在构建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中保持不变。 我该怎么办?
非常感谢!
更改ListView的'layout_height'到'match_parent',并删除了滚动。您也可以摆脱列表项目布局中的其中一个RelativeLayouts。 –
我做了你所说的。现在我有一个带有ListView的AlertDialog,但问题是对话框与整个屏幕一样大。有没有办法让它变得有点狡猾? – Asaak
在LinearLayout上设置精确的高度测量。或者将它的'layout_height'改为'wrap_content',并在ListView上设置一个精确的高度尺寸。另外,如果你不打算在LinearLayout中添加更多的视图,你并不需要它。 –