我创建了一个自定义对话框片段及其XML,如下所示:

<?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="wrap_content"
    android:layout_margin="5dp"

    android:padding="5dip" >

    <ListView
        android:id="@+id/product_list"
         android:background="@drawable/border_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

    <RelativeLayout
        android:id="@+id/AddtoCart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="10dp"

        android:background="#005959" >

        <Button
            android:id="@+id/button_addToCart"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/gradient_button"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:text="@string/add_to_cart"
            android:textColor="#000000" />
    </RelativeLayout>

</RelativeLayout>

当我调用此对话框时,它将全屏显示,如屏幕截图所示:
如何将此对话框的大小限制为列表和按钮的高度?
如何删除多余的空白?

最佳答案

通过将列表的RelativeLayout更改为LinearLayout并将weight设置为1来解决。
如下所示

<?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="wrap_content"
    android:layout_margin="5dp"
   android:orientation="vertical"
    android:padding="5dip" >

    <ListView
        android:id="@+id/product_list"
         android:background="@drawable/border_details"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
         />
    <RelativeLayout
    android:id="@+id/AddtoCart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_marginTop="10dp"
    android:background="#005959" >

    <Button
        android:id="@+id/button_addToCart"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginBottom="2dp"
        android:layout_marginTop="2dp"
        android:background="@drawable/gradient_button"
        android:text="@string/add_to_cart"
        android:textColor="#000000" />

</RelativeLayout>
</LinearLayout>

07-28 01:11
查看更多