自定义AlertDialog大小太小

时间:2013-08-27 19:35:15

标签: android android-alertdialog

我正在尝试构建一个简单的自定义警报对话框。只是标题,编辑文本和按钮。问题是对话框太小而无法正确显示小部件。我试图实施相当多的建议,但没有一个能解决问题。如何将其扩展到更大的尺寸?我究竟做错了什么?谢谢!

我的代码是:

LayoutInflater inflater = LayoutInflater.from(this);
View builderView = inflater.inflate(R.layout.enter_password, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(builderView);   
AlertDialog alert = builder.create();   
alert.show();

布局xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="@drawable/background_gray_gradient" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="70dp"
            android:layout_gravity="left"

            android:contentDescription="@string/blank"
            android:src="@raw/lock" >
        </ImageView>    

        <TextView
            android:layout_marginTop="15dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:paddingTop="5dip"
            android:paddingLeft="5dp"
            android:text="@string/password_to_continue_title"
            android:textSize="20sp" />

    </LinearLayout>

    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:hint="@string/password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
            android:id="@+id/submit_password"
            style="@style/ButtonText"
            android:onClick="submitPassword"
            android:layout_gravity="center_horizontal"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/gray_button"
            android:text="@string/continue_on" />

</LinearLayout>

1 个答案:

答案 0 :(得分:3)

这个答案只是满足您的要求,https://stackoverflow.com/a/10912076/826657

       Rect displayRectangle = new Rect();
        Window window = this.getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
        LayoutInflater inflater = LayoutInflater.from(this);
        View builderView = inflater.inflate(R.layout.activity_dia, null);
        builderView.setMinimumWidth((int) (displayRectangle.width() * 0.9f));
        builderView.setMinimumHeight((int) (displayRectangle.height() * 0.9f));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(builderView);
        AlertDialog alert = builder.create();
        alert.show();