【发布时间】:2023-01-26 18:55:40
【问题描述】:
我想在底部的警告对话框中添加一个分隔符,类似于它在有长列表视图时显示的分隔符(见图)。 但是,我添加了自定义视图,因为我希望能够为整个应用程序中的所有 AlertDialogs 使用通用布局。现在视图和按钮之间有一个非常大的空间。此外,曾经有分隔线(来自原始 AlertDialog 视图)的列表视图不再可用,我的布局有它自己的回收视图。 (因为现在推荐它们。) 我尝试在我的自定义视图中添加一个分隔线,但是这个分隔线绑定到它的父视图的填充,因此不会填充整个对话框(并且仍然与按钮间隔很远)。
所以我基本上想访问警报对话框中的按钮区域,以在此视图的顶部添加分隔线。 (这将是全宽并低于自定义视图的填充。)如果 Builder 可以做到这一点,那就太好了,因为我有时使用正负按钮,有时只使用负按钮或只使用正按钮,并自定义所有这些通用布局也将是一项巨大的努力。
这是我的自定义布局(带分隔线)。我正在使用 MaterialAlertDialogBuilder。注意:我可以移除底部的填充以移除空间,但它看起来被砸碎了,分隔线的宽度问题仍然没有解决。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
style="@style/AppCompatAlertDialogTheme"
android:padding="?dialogPreferredPadding"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/alert_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="to"
android:textAppearance="@style/TFAlertDialogStyle"
android:textColor="@color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/alert_explanation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_small"
android:text="@string/lorem"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/alert_title"
tools:visibility="visible" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/alert_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/alert_explanation">
<!--In here will be the options for every alert dialog-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/alert_options_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:id="@+id/alertDivider"
style="@style/Divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
编辑 1:它也应该与正在填充的 recyclerview 一起工作,所以它必须滚动,当前的解决方案让 recyclerview 在填充中消失。还剩 1 1/2 项,但我无法再滚动了。 (图像在 Divider 上没有 marginTop,有边距它实际上也消失了)
解决方案: 如果您没有包含很多项目的滚动回收视图,@shraddha patel 的方法就可以了。然而,如果你有一个,为了避免切断最后的项目,你需要为内容视图使用 LinearLayout 而不是 ConstraintLayout,如这个有点相关的错误报告所示:https://github.com/material-components/material-components-android/issues/1336 但是,如果你在 recyclerview 下面有项目,父布局仍然需要保持约束布局,否则分隔线将在滚动列表中消失。 所以现在对我来说可行的解决方案是:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Don't ever refactor that to constraint layout.
There's some weird magic making it work only with a LinearLayout.-->
<androidx.appcompat.widget.LinearLayoutCompat
android:orientation="vertical"
android:id="@+id/alert_content_view"
style="@style/AppCompatAlertDialogTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="?dialogPreferredPadding">
<TextView
android:id="@+id/alert_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="@style/TFAlertDialogStyle"
android:textColor="@color/black"/>
<TextView
android:id="@+id/alert_explanation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_small"
android:text="@string/desc"
android:textColor="@color/black"
tools:visibility="visible" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/alert_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_small">
<!--In here will be the options for every alert dialog-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/alert_options_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
//Some views set to visibility = gone
</androidx.appcompat.widget.LinearLayoutCompat>
<View
android:id="@+id/alertDivider"
style="@style/Divider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/alert_content_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
标签: android android-alertdialog divider