1

我正在构建一个警报对话框来制作预付款摘要。问题是这个警报对话框占用了所有屏幕而不是包装内容。

我试过了:

  • 将我的布局的第一个视图的 layout_height 和 layout_width 转换为wrap_content
  • 使用AlertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE)

在此处输入图像描述

活动代码:

LayoutInflater inflater = BasketActivity.this.getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_pre_payment, null, false);

/* Set all TextView values here ....*/

Builder builder = new Builder(BasketActivity.this);
builder.setView(layout);

/* Avoid Double Pop-up */
if (payDialog != null && payDialog.isShowing()) {
    payDialog.dismiss();
}

payDialog = builder.create();
            payDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            payDialog.show();

dialog_pre_payment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border_grey"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout 
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="10"
    android:paddingBottom="10dp">
    <LinearLayout 
        android:orientation="vertical"
        android:layout_height="match_parent"
                android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp">
            <TextView android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/basket"
                style="@style/list_key"
                android:textStyle="bold"
                android:layout_marginTop="20dp"/>
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:weightSum="1"
            android:background="@drawable/border_grey"
            android:layout_marginTop="5dp"
                 android:padding="15dp"
                 >
             <ListView android:id="@+id/priced_basket"
                 android:layout_weight="1"
                 android:layout_width="match_parent"
                 android:layout_height="0dp"

                 >

             </ListView>
             <LinearLayout android:layout_width="match_parent"
                android:layout_height="1dp"
                android:orientation="vertical"
                android:background="@color/login_input_border"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="1dp"
                android:gravity="bottom"/>
             <LinearLayout android:orientation="horizontal"
                 android:layout_height="wrap_content"
                 android:layout_width="match_parent"
                 android:gravity="bottom">
                 <TextView android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:text="Total:"
                     android:textStyle="bold"
                     android:textColor="@color/main_fg"
                     android:textSize="16sp"/>
                <TextView android:id="@+id/total_priced_list" 
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:textStyle="bold"
                    android:gravity="right"
                    android:textColor="@color/main_fg"
                    android:textSize="16sp"/>
             </LinearLayout>
         </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
         android:layout_weight="5"
         android:orientation="vertical">
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="@string/beneficiary"
            android:textStyle="bold"
            android:layout_marginTop="20dp"
            style="@style/list_key"/>
        <LinearLayout android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:background="@drawable/border_grey"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:padding="15dp">
            <ImageView android:id="@+id/customer_pic"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/user_pic"/>

            <LinearLayout android:orientation="vertical"
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" >
                <TextView android:id="@+id/card_name"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    style="@style/list_key"
                    android:textColor="@color/main_fg"/>
                <TextView android:id="@+id/card_email"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    style="@style/list_key" 
                    android:textColor="@color/main_fg"/>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
             android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/border_grey"
            android:layout_marginRight="10dp"
            android:layout_marginTop="25dp"
            android:padding="15dp"
            >
            <LinearLayout android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView  android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/balance"
                    android:textColor="@color/main_fg"
                    android:textSize="16sp"/>
                <TextView android:id="@+id/card_balance"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:gravity="right"
                    android:textSize="16sp"/>
            </LinearLayout>
           <LinearLayout android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView  android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/amount"
                    android:textColor="@color/main_fg"
                    android:textSize="16sp"/>
                <TextView android:id="@+id/basket_total_price"
                    android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                    android:gravity="right"
                    android:textColor="@color/neutral"
                    android:textSize="16sp"/>
            </LinearLayout>
            <LinearLayout android:layout_width="match_parent"
                android:layout_height="1dp"
                android:orientation="vertical"
                android:background="@color/login_input_border"/>
           <LinearLayout android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
                <TextView  android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/previsional_balance"
                    android:textColor="@color/main_fg"
                    android:textSize="16sp"/>

                <TextView android:id="@+id/card_balance_after_payment"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:gravity="right"
                    android:layout_gravity="right"
                    android:textSize="16sp"/>
            </LinearLayout>
    </LinearLayout>
</LinearLayout>

</LinearLayout>
    <LinearLayout android:orientation="horizontal"
    android:layout_height="40dp"
    android:layout_width="match_parent"
    android:weightSum="2"
    android:gravity="center_horizontal">
    <Button android:id="@+id/btn_cancel"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal" 
        android:text="@string/cancel"
        style="@style/CheckBalanceButton"
        />
    <Button android:id="@+id/btn_validate"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal" 
        android:text="@string/pay"
        style="@style/PayButton"/>
</LinearLayout>

4

2 回答 2

1

阿尔布兰克。在您的实际代码周围添加一个虚拟的Relativelayout,高度和宽度为match_parent。我一直使用这种技术。这是您修改后的代码,可以给您一个想法。

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

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_centerInParent="true"
    android:background="@drawable/border_grey"
    android:orientation="vertical"
    android:weightSum="1" >

<LinearLayout 
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="10"
    android:paddingBottom="10dp">
    <LinearLayout 
        android:orientation="vertical"
        android:layout_height="match_parent"
                android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp">
            <TextView android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/basket"
                style="@style/list_key"
                android:textStyle="bold"
                android:layout_marginTop="20dp"/>
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:weightSum="1"
            android:background="@drawable/border_grey"
            android:layout_marginTop="5dp"
                 android:padding="15dp"
                 >
             <ListView android:id="@+id/priced_basket"
                 android:layout_weight="1"
                 android:layout_width="match_parent"
                 android:layout_height="0dp"

                 >

             </ListView>
             <LinearLayout android:layout_width="match_parent"
                android:layout_height="1dp"
                android:orientation="vertical"
                android:background="@color/login_input_border"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="1dp"
                android:gravity="bottom"/>
             <LinearLayout android:orientation="horizontal"
                 android:layout_height="wrap_content"
                 android:layout_width="match_parent"
                 android:gravity="bottom">
                 <TextView android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:text="Total:"
                     android:textStyle="bold"
                     android:textColor="@color/main_fg"
                     android:textSize="16sp"/>
                <TextView android:id="@+id/total_priced_list" 
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:textStyle="bold"
                    android:gravity="right"
                    android:textColor="@color/main_fg"
                    android:textSize="16sp"/>
             </LinearLayout>
         </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
         android:layout_weight="5"
         android:orientation="vertical">
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="@string/beneficiary"
            android:textStyle="bold"
            android:layout_marginTop="20dp"
            style="@style/list_key"/>
        <LinearLayout android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:background="@drawable/border_grey"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:padding="15dp">
            <ImageView android:id="@+id/customer_pic"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/user_pic"/>

            <LinearLayout android:orientation="vertical"
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" >
                <TextView android:id="@+id/card_name"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    style="@style/list_key"
                    android:textColor="@color/main_fg"/>
                <TextView android:id="@+id/card_email"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    style="@style/list_key" 
                    android:textColor="@color/main_fg"/>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
             android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/border_grey"
            android:layout_marginRight="10dp"
            android:layout_marginTop="25dp"
            android:padding="15dp"
            >
            <LinearLayout android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView  android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/balance"
                    android:textColor="@color/main_fg"
                    android:textSize="16sp"/>
                <TextView android:id="@+id/card_balance"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:gravity="right"
                    android:textSize="16sp"/>
            </LinearLayout>
           <LinearLayout android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView  android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/amount"
                    android:textColor="@color/main_fg"
                    android:textSize="16sp"/>
                <TextView android:id="@+id/basket_total_price"
                    android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                    android:gravity="right"
                    android:textColor="@color/neutral"
                    android:textSize="16sp"/>
            </LinearLayout>
            <LinearLayout android:layout_width="match_parent"
                android:layout_height="1dp"
                android:orientation="vertical"
                android:background="@color/login_input_border"/>
           <LinearLayout android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
                <TextView  android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/previsional_balance"
                    android:textColor="@color/main_fg"
                    android:textSize="16sp"/>

                <TextView android:id="@+id/card_balance_after_payment"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:gravity="right"
                    android:layout_gravity="right"
                    android:textSize="16sp"/>
            </LinearLayout>
    </LinearLayout>
</LinearLayout>

</LinearLayout>
    <LinearLayout android:orientation="horizontal"
    android:layout_height="40dp"
    android:layout_width="wrap_content"
    android:weightSum="2"  
    android:gravity="center_horizontal">
    <Button android:id="@+id/btn_cancel"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal" 
        android:text="@string/cancel"
        style="@style/CheckBalanceButton"
        />
    <Button android:id="@+id/btn_validate"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal" 
        android:text="@string/pay"
        style="@style/PayButton"/>
</LinearLayout>

</RelativeLayout>
于 2013-06-04T16:43:02.557 回答
1

不要调用实例上的方法,而是调用实例setView上的方法。像这样:AlertDialog.BuildersetViewAlertDialog

payDialog = builder.create();
payDialog.setView(layout, 0, 0, 0, 0);
payDialog.show();

这应该将对话框上的填充设置为零。

于 2013-06-04T16:56:11.550 回答