android - 如何在 MaterialAlertDialogBuilder 中的视图周围添加边距?
问题描述
当用户单击特定项目(从列表中)时,我需要用户提供更多信息。我正在显示一个对话框以获取更多信息,并且我正在使用 MaterialAlertDialogBuilder。这就是我构建它的方式:
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>
正如您在以下屏幕截图中看到的那样,由于某种原因,上述边距被忽略了。有谁知道为什么以及如何在视图周围添加边距?
解决方案
在你的观点中使用不同的东西。
就像是:
<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();
推荐阅读
- sql - SQL Anywhere 错误 - 对“分数”的函数或列引用也必须出现在 GROUP BY 中
- reactjs - 反应无法获取到数组
- html - vba:如何单击 iframe 中的元素
- python - 如何使用 numpy linalg 求解器进行多维使用?
- java - 为什么这段代码打印超类而不是子类的值
- php - 我的代码无法将数据发送到我的 mysqli 数据库,但它没有产生错误
- react-native - 使用任何 react-native 库创建一个简单的 ImageSlider
- javascript - 您的会话已过期。在 ajax 表单上发布 laravel
- javascript - ReactJS 为子组件添加回调函数
- c# - 将 SqlParameters 的动态数组传递到 SQL 字符串 IN 子句