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

我的代码是:

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>

最佳答案

这个答案正好满足您的要求,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();

08-06 11:29