Android 自定义对话框大小不成比例

Android Custom Dialog size not proportional

我需要帮助来进一步了解自定义对话框。

我创建了自定义对话框,但为什么大小不成比例,就像这样..

这就是我调用自定义布局对话框的方式。

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_dp3_out); //layout for dialog
dialog.setTitle("Add a new friend");

dialog.show();

这是自定义布局...

    <?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="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_time_load"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <EditText
                android:id="@+id/txtTimeLoad"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Jam Muat Dari"
                android:inputType="time"
                android:maxLines="1"
                android:textColor="#000000" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_time_finish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <EditText
                android:id="@+id/txtTimeFinish"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Jam Muat Sampai"
                android:inputType="time"
                android:maxLines="1"
                android:textColor="#000000" />
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_from"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <EditText
            android:id="@+id/txtFrom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Asal"
            android:maxLines="1"
            android:textColor="#000000" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_to"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <EditText
            android:id="@+id/txtTo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Tujuan"
            android:maxLines="1"
            android:textColor="#000000" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_vehicle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <EditText
            android:id="@+id/txtVehicle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Nomor Kendaraan"
            android:maxLines="1"
            android:textColor="#000000" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_vehicle_driver"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <EditText
            android:id="@+id/txtVehicleDriver"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Supir"
            android:maxLines="1"
            android:textColor="#000000" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_vehicle_codriver"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <EditText
            android:id="@+id/txtVehicleCodriver"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Kenek"
            android:maxLines="1"
            android:textColor="#000000" />
    </android.support.design.widget.TextInputLayout>
</LinearLayout>

请让我知道我哪里错了..问候..

你应该使用setLayout方法

 Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_dp3_out);
dialog.getWindow().setLayout( , ); // Add  Width and Height
dialog.show();

动态

DisplayMetrics metrics = getResources().getDisplayMetrics();

    int device_TotalWidth = metrics.widthPixels;
    int device_TotalHeight = metrics.heightPixels;

   dialog.getWindow().setLayout(device_TotalWidth*80/100 , ); // set here your value