自定义AlertDialog正在进行所有屏幕

时间:2013-06-04 16:23:20

标签: android alertdialog

我正在构建一个alertdialog来进行预付费摘要。问题是这个alertdialog占用了所有的屏幕,而不是包装内容。

我试过了:

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

enter image description here

活动代码:

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>

2 个答案:

答案 0 :(得分:1)

Al Blanc。使用高度和宽度match_parent在实际代码周围添加一个虚拟Relativelayout。我总是使用这种技术。 这是您修改后的代码,可以为您提供一个想法。

<?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>

答案 1 :(得分:1)

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

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

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