带有 ConstraintLayout 的 BottomSheetDialog 包含 NestedScrollView,其高度 match_constraint 渲染为实际 0px 高度

Dav*_*oni 5 android android-scrollview android-constraintlayout android-bottomsheetdialog

我有一个非常简单的布局,需要将其渲染为BottomSheetDialog. 布局只是一个滚动视图,仅包含TextViews 和下面的一个按钮一个按钮(因此该按钮固定在底部并且始终可见。)\n这只是我的应用程序中内容的精炼版本,但它会足以说明问题:

\n
<?xml version="1.0" encoding="utf-8"?>\n<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    android:layout_height="match_parent"\n    android:layout_width="match_parent">\n\n    <androidx.constraintlayout.widget.ConstraintLayout\n        android:layout_width="match_parent"\n        android:layout_height="match_parent">\n\n        <androidx.core.widget.NestedScrollView\n            android:id="@+id/nestedScrollView"\n            android:fillViewport="true"\n            android:layout_width="0dp"\n            android:layout_height="0dp"\n            android:layout_marginBottom="16dp"\n            app:layout_constraintBottom_toTopOf="@id/ok_button"\n            app:layout_constraintEnd_toEndOf="parent"\n            app:layout_constraintStart_toStartOf="parent"\n            app:layout_constraintTop_toTopOf="parent">\n\n        <androidx.constraintlayout.widget.ConstraintLayout\n            android:layout_width="match_parent"\n            android:layout_height="match_parent">\n\n                <TextView\n                    android:id="@+id/fine_print_header"\n                    android:layout_width="match_parent"\n                    android:layout_height="wrap_content"\n                    android:layout_marginStart="16dp"\n                    android:layout_marginLeft="16dp"\n                    android:layout_marginTop="32dp"\n                    android:layout_marginEnd="16dp"\n                    android:gravity="center_horizontal"\n                    android:text="Fine Print"\n                    app:layout_constraintEnd_toEndOf="parent"\n                    app:layout_constraintStart_toStartOf="parent"\n                    app:layout_constraintTop_toTopOf="parent" />\n\n                <TextView\n                    android:id="@+id/fine_print_copy"\n                    android:layout_width="match_parent"\n                    android:layout_height="wrap_content"\n                    android:layout_marginStart="16dp"\n                    android:layout_marginLeft="16dp"\n                    android:layout_marginTop="32dp"\n                    android:layout_marginEnd="16dp"\n                    android:text="This is the fine print. It\'s very detailed. So many carefully crafted details"\n                    app:layout_constraintEnd_toEndOf="parent"\n                    app:layout_constraintStart_toStartOf="parent"\n                    app:layout_constraintTop_toBottomOf="@id/fine_print_header"/>\n\n            </androidx.constraintlayout.widget.ConstraintLayout>\n        </androidx.core.widget.NestedScrollView>\n        <Button\n            android:id="@+id/ok_button"\n            android:layout_width="0dp"\n            android:layout_height="wrap_content"\n            android:layout_marginHorizontal="16dp"\n            android:layout_marginVertical="16dp"\n            android:text="OK"\n            app:layout_constraintBottom_toBottomOf="parent"\n            app:layout_constraintEnd_toEndOf="parent"\n            app:layout_constraintStart_toStartOf="parent"\n            app:layout_constraintTop_toBottomOf="@id/nestedScrollView"/>\n    </androidx.constraintlayout.widget.ConstraintLayout>>\n</androidx.coordinatorlayout.widget.CoordinatorLayout>\n
Run Code Online (Sandbox Code Playgroud)\n

这是我在我的中呈现布局的方式Fragment

\n
<?xml version="1.0" encoding="utf-8"?>\n<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    android:layout_height="match_parent"\n    android:layout_width="match_parent">\n\n    <androidx.constraintlayout.widget.ConstraintLayout\n        android:layout_width="match_parent"\n        android:layout_height="match_parent">\n\n        <androidx.core.widget.NestedScrollView\n            android:id="@+id/nestedScrollView"\n            android:fillViewport="true"\n            android:layout_width="0dp"\n            android:layout_height="0dp"\n            android:layout_marginBottom="16dp"\n            app:layout_constraintBottom_toTopOf="@id/ok_button"\n            app:layout_constraintEnd_toEndOf="parent"\n            app:layout_constraintStart_toStartOf="parent"\n            app:layout_constraintTop_toTopOf="parent">\n\n        <androidx.constraintlayout.widget.ConstraintLayout\n            android:layout_width="match_parent"\n            android:layout_height="match_parent">\n\n                <TextView\n                    android:id="@+id/fine_print_header"\n                    android:layout_width="match_parent"\n                    android:layout_height="wrap_content"\n                    android:layout_marginStart="16dp"\n                    android:layout_marginLeft="16dp"\n                    android:layout_marginTop="32dp"\n                    android:layout_marginEnd="16dp"\n                    android:gravity="center_horizontal"\n                    android:text="Fine Print"\n                    app:layout_constraintEnd_toEndOf="parent"\n                    app:layout_constraintStart_toStartOf="parent"\n                    app:layout_constraintTop_toTopOf="parent" />\n\n                <TextView\n                    android:id="@+id/fine_print_copy"\n                    android:layout_width="match_parent"\n                    android:layout_height="wrap_content"\n                    android:layout_marginStart="16dp"\n                    android:layout_marginLeft="16dp"\n                    android:layout_marginTop="32dp"\n                    android:layout_marginEnd="16dp"\n                    android:text="This is the fine print. It\'s very detailed. So many carefully crafted details"\n                    app:layout_constraintEnd_toEndOf="parent"\n                    app:layout_constraintStart_toStartOf="parent"\n                    app:layout_constraintTop_toBottomOf="@id/fine_print_header"/>\n\n            </androidx.constraintlayout.widget.ConstraintLayout>\n        </androidx.core.widget.NestedScrollView>\n        <Button\n            android:id="@+id/ok_button"\n            android:layout_width="0dp"\n            android:layout_height="wrap_content"\n            android:layout_marginHorizontal="16dp"\n            android:layout_marginVertical="16dp"\n            android:text="OK"\n            app:layout_constraintBottom_toBottomOf="parent"\n            app:layout_constraintEnd_toEndOf="parent"\n            app:layout_constraintStart_toStartOf="parent"\n            app:layout_constraintTop_toBottomOf="@id/nestedScrollView"/>\n    </androidx.constraintlayout.widget.ConstraintLayout>>\n</androidx.coordinatorlayout.widget.CoordinatorLayout>\n
Run Code Online (Sandbox Code Playgroud)\n

期待什么看到的是 Android Studio 预览中的内容:

\n

Android Studio 预览

\n

实际上是什么看到的是这样的:

\n

应用程序屏幕截图

\n

如果我使用Layout Inspector介入,我可以看到NestedScrollView确实存在,但它是用 height 渲染的0px。就好像约束布局工具忽略了整个match_constraint想法并真正渲染了东西,就好像我要求0dp. 如果我使用match_parent它将呈现在按钮后面;如果我使用wrap_content预览会显示它在边界之外渲染。如果我设置实际大小(例如80dp),那么它将出现: 但这不是一个可扩展的解决方案 \xe2\x80\x93 我应该能够使用match_constraint在这里使用!

\n

NestedScrollView我尝试了和之间的 top:bottom 约束关系的所有相关组合Button,尝试使用和不使用android:fillViewPort="true",尝试使用和不使用app:layout_behavior=\xe2\x80\x9c@string/appbar_scrolling_view_behavior\xe2\x80\x9d,尝试不使用CoordinatorLayouta 作为顶部布局(使用ConstraintLayout在顶部使用 a 代替) \xe2\x80\x93 none其中有任何影响。

\n

有人知道如何处理这个问题吗?

\n

更新:

\n

我能够让一些事情发挥作用(不必“承认失败”并切换到RelativeLayout。)NestedScrollViewI 集上:

\n
            android:layout_height="wrap_content"\n            app:layout_constrainedHeight="true"\n
Run Code Online (Sandbox Code Playgroud)\n

现在它在预览和设备中都能正确呈现。

\n

还是想知道为什么 MATCH_CONSTRAINT 在这种情况下不起作用。

\n