我想在 AlertDialog 中显示一个列表,列表下方有一个按钮。 Button 用于关闭 Dialog 本身。我扩展了 AlertDialog
并在 Window
中添加了一些 LayoutParams
以扩展整个屏幕宽度的 Dialog:
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
好的。但是如果列表很长(显示满了它),我可以滚动 ListView
但按钮不会显示在它下面。这是对话框的 ContentView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_below="@+id/choose_equipment_list"
android:id="@+id/btn_choose_equipment_close"
style="@style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</RelativeLayout>
我该怎么做才能始终在列表下方显示按钮,无论它有多长? (我读了很多关于将 ListView
放入 ScrollView
的警告,无论如何都尝试过但失败了...)
最佳答案
试一试:
<?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"
android:orientation="vertical" >
<ListView
android:id="@+id/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/btn_choose_equipment_close"
style="@style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
如果不起作用,请删除 style="@style/custom_button"
行。一些样式配置可以改变 View 样式并隐藏按钮。
关于android - AlertDialog中ListView下的按钮被隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21687213/