从AlertDialog中删除不必要的空间

时间:2017-01-11 15:18:07

标签: android android-view android-alertdialog

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

enter image description here

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

enter image description here

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>

爪哇:

          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(); 

3 个答案:

答案 0 :(得分:1)

删除此行:

alertDialogBuilderComplete.setMessage("")

或将其更改为:

alertDialogBuilderComplete.setMessage(null)

答案 1 :(得分:0)

发布xml后,我看到TurnBasedGameField将其设为android:layout_marginTop="20dp"

android:layout_marginTop="0dp"

答案 2 :(得分:0)

id