如何在MaterialAlertDialogBu​​ilder中的视图周围添加边距?

当用户单击特定项目(从列表中)时,我需要他们提供更多信息。我正在显示一个对话框以获取更多信息,并且我正在使用MaterialAlertDialogBu​​ilder。这就是我的构建方式:

val v = LayoutInflater.from(context).inflate(R.layout.view_text_input,null)
MaterialAlertDialogBuilder(context,R.style.AlertDialogMaterialTheme)
                .setTitle(R.string.ofi_pleaseDescribe)
                .setView(v)
                .setPositiveButton(R.string.ok) { d,i ->
                    if (d == null) return@setPositiveButton
                    val txt = etInput.text?.trim()
                    etRationale.setText(txt)
                }
                .show()

这是其布局(view_text_input.xml)。

<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/ofi_textInputLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="16dp"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/etInput"
        style="@style/ofi_editText"
        android:hint="@string/ofi_title_hint" />

</com.google.android.material.textfield.TextInputLayout>

如下面的屏幕截图所示,由于某些原因,上述边距被忽略了。有谁知道为什么以及如何在视图周围添加边距?

如何在MaterialAlertDialogBu​​ilder中的视图周围添加边距?

yghe001 回答:如何在MaterialAlertDialogBu​​ilder中的视图周围添加边距?

在您的视图中使用其他内容。

类似的东西:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="?attr/dialogPreferredPadding"
    android:paddingStart="?attr/dialogPreferredPadding"
    android:paddingEnd="?attr/dialogPreferredPadding">

  <com.google.android.material.textfield.TextInputLayout
      android:id="@+id/text_input_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      ...>

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        .../>
  </com.google.android.material.textfield.TextInputLayout>

</FrameLayout>

然后:

new MaterialAlertDialogBuilder(AlertDialogActivity.this)
            .setTitle(R.string.ofi_pleaseDescribe)
            .setView(R.layout.view_text_input)
            ...
            .show();

enter image description here

本文链接:https://www.f2er.com/3093427.html

大家都在问