在警告对话框中,如何减少 'message' 和我的视图之间的间距?

In an alert dialog, how can I reduce the spacing between the 'message' and my view?

例如下面的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("foo");
builder.setMessage("bar");
EditText edit = new EditText(activity);
builder.setView(edit);
builder.show();

Results in this dialog

可以看出,消息和编辑文本之间有一个很大的垂直 space。我想减少它,理想情况下不必使用自定义布局。

如果我们在模拟器中显示 AlertDialog 并在布局检查器中显示组件树,我们会看到额外的 space 来自 Space 名为“textSpacerNoButtons”的小部件

和一个名为“customPanel”的 FrameLayout,其中包含 EditText

Space 小部件的指定高度为 18dp(稍后我们将看到),而 FrameLayout的最小高度为48dp,可以在布局检查器的属性中看到。

AlertDialog 使用的布局是 alert_dialog_material.xml。我已经复制了以下相关部分:

<FrameLayout
    android:id="@+id/contentPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Space
                android:id="@+id/textSpacerNoTitle"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dialog_padding_top_material" />

            <TextView
                android:id="@+id/message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingEnd="?attr/dialogPreferredPadding"
                android:paddingStart="?attr/dialogPreferredPadding"
                style="@style/TextAppearance.Material.Subhead" />

            <Space
                android:id="@+id/textSpacerNoButtons"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dialog_padding_top_material" />
        </LinearLayout>
    </ScrollView>
</FrameLayout>

<FrameLayout
    android:id="@+id/customPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp">

    <FrameLayout
        android:id="@+id/custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</FrameLayout>

对于 Space 小部件,高度定义为 @dimen/dialog_padding_top_material,在示例中为 18dp。更改此值还有其他含义,因此我们将保留它。

那么,这就是 space 的来源,但是如何解决呢?我更喜欢的方式是使用自定义视图,即使它是“alert_dialog_material.xml”的近乎精确的副本。如果这不可接受,那么我们必须即时进行更改以删除 contentPanel 的最小高度并设置 Space 小部件的可见性 gone.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("foo");
builder.setMessage("bar");
EditText edit = new EditText(this);
builder.setView(edit);
AlertDialog alertDialog = builder.show();
View decorview = alertDialog.getWindow().getDecorView();
View view = decorview.findViewById(R.id.contentPanel);
view.setMinimumHeight(0);
view = decorview.findViewById(R.id.textSpacerNoButtons);
view.setVisibility(View.GONE);