从 AlertDialog 中删除不必要的 space

Remove unnecessary space from AlertDialog

我有一个 AlertDialog 设置了自定义视图。在 Android Studio 的布局预览中,它显示了我期望的布局外观:

然而,当应用程序在我的 Nexus 6P 上运行时,它看起来像这样(注意顶部额外的 space,那是我不想要的):

As suggested here 我尝试将布局设置更改为:

alertDialog2.setView(view2,0,0,0,0);
alertDialog2.show();

然而这并没有解决我的问题。关于如何实现这一点有什么建议吗?

XML布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:text="Suggest improvements or changes, or report bugs."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:id="@+id/textView5"
    android:textColor="@color/Black"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="45dp">

    <EditText
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/email_feedback_text"
        android:layout_marginLeft="125dp"
        android:layout_width="250dp"
        android:layout_alignParentBottom="false"
        android:layout_marginRight="5dp" />

    <TextView
        android:text="E-mail*:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/textV5"
        android:textColor="@color/Black"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="30dp" />
</RelativeLayout>
</RelativeLayout>

Java:

          AlertDialog.Builder alertDialogBuilderComplete = new AlertDialog.Builder(
                                MainActivity.this);

                        alertDialogBuilderComplete.setCancelable(false);
                        alertDialogBuilderComplete.setTitle("Submit Feedback"); //Set the title of the box
                        alertDialogBuilderComplete.setMessage("");
                        alertDialogBuilderComplete.setNegativeButton("Submit",null);
                        alertDialogBuilderComplete.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel(); //when they click dismiss we will dismiss the box
                            }
                        });
                        alertDialog2 = alertDialogBuilderComplete.create(); //create the box
                        alertDialog2.setOnShowListener(new DialogInterface.OnShowListener(){
                            @Override
                            public void onShow(DialogInterface dialog) {
                                Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
                                button.setOnClickListener(new View.OnClickListener() {
                                    }
                                });
                            }
                        });

                        alertDialog2.setView(view2,0,0,0,0);
                        alertDialog2.show(); 

在你发布你的 xml 之后,我看到 android:layout_marginTop="20dp" 成功了 android:layout_marginTop="0dp"

<TextView
    android:text="Suggest improvements or changes, or report bugs."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="0dp"
    android:id="@+id/textView5"
    android:textColor="@color/Black"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
android:text="Suggest improvements or changes, or report bugs."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
// remove margin top
android:id="@+id/textView5"
android:textColor="@color/Black"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
  ........
         .......
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
// remove margin top, can be added margin as needed

<EditText
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/email_feedback_text"
    android:layout_marginLeft="125dp"
    android:layout_width="250dp"
    android:layout_alignParentBottom="false"
    android:layout_marginRight="5dp" />
     ......
           ..........
     </RelativeLayout>

删除这一行:

alertDialogBuilderComplete.setMessage("")

或改为:

alertDialogBuilderComplete.setMessage(null)