android - AlertDialog中ListView下的按钮被隐藏

标签 android listview scrollview scrollable

我想在 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/

相关文章:

android - 如何在使用 appcompat21 时隐藏抽屉导航指示器

android - 本地服务还是远程服务?

java.lang.OutOfMemory 错误 - SD 卡图像导入 ImageView

android - 使用 Angular 切换 nativescript 中的 ListView 内容

android - ScrollView 不会显示所有内容

react-native - ScrollView 不显示内容 React-native

java - Android 应用程序无法更改 JAVA 中 HTTP 连接的响应代码

java - 安卓 : getting multiple data in listview from server in another screen using Json

android - ListView,在Android中使用颜色代码数组更改每一行

android - 如何阻止 ScrollView 滚动到 WebView 中当前加载页面的底部