Android 如何在MaterialAlertDialogBuilder中为视图添加边距?
当用户单击某个特定项目(从列表中)时,我需要用户提供更多信息。我正在显示一个对话框以获取更多信息,我正在使用MaterialAlertDialogBuilder。这就是我构建它的方式:Android 如何在MaterialAlertDialogBuilder中为视图添加边距?,android,android-alertdialog,android-textinputlayout,material-components,material-components-android,Android,Android Alertdialog,Android Textinputlayout,Material Components,Material Components Android,当用户单击某个特定项目(从列表中)时,我需要用户提供更多信息。我正在显示一个对话框以获取更多信息,我正在使用MaterialAlertDialogBuilder。这就是我构建它的方式: val v = LayoutInflater.from(context).inflate(R.layout.view_text_input, null) MaterialAlertDialogBuilder(context, R.style.AlertDialogMaterialTheme)
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\u text\u input.xml)
正如您在下面的屏幕截图中所看到的,由于某些原因,上面的边距被忽略了。有人知道为什么以及如何在视图周围添加边距吗
在你的观点中使用不同的东西 比如:
<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>
从什么角度增加了保证金?尝试添加任何布局作为rootoh,谢谢。它很有魅力。似乎我们不能将TextInputLayout用作根视图。@他回答是的,您可以将TextInputLayout用作根视图。外部框架布局实际上没有什么用处。
<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();