为什么添加约束后NestedScrollView仍无法显示在ConstraintLayout下方
问题根因
你当前布局未生效的核心原因是ConstraintLayout下的尺寸配置规则使用错误,调整2处配置即可实现需求:
1. 调整外层NestedScrollView属性
将paymentRollover的高度属性从match_parent改为0dp(ConstraintLayout中代表匹配约束空间),同时补充底部到父布局的约束,修改后代码如下:
<androidx.core.widget.NestedScrollView android:id="@+id/paymentRollover" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/paymentDescriptor" app:layout_constraintBottom_toBottomOf="parent" android:fillViewport="true">
修改逻辑说明:ConstraintLayout中match_parent不会遵循你设置的顶部对齐下方控件的约束,会直接占满整个父布局高度,容易和上方paymentDescriptor重叠。设置为0dp加上下约束后,控件会自动填充paymentDescriptor下方到父布局底部的所有剩余空间。
2. 调整NestedScrollView内部ConstraintLayout属性
将availablePayments的高度属性从match_parent改为wrap_content,同时删除无效的约束属性,修改后代码如下:
<androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/availablePayments" android:layout_width="match_parent" android:layout_height="wrap_content">
修改逻辑说明:NestedScrollView的直接子View不需要设置对齐父布局的约束,这类约束不会生效。设置高度为wrap_content能让内容正常撑开高度,配合你已开启的fillViewport属性,内容不足一屏时会自动填充满ScrollView可视区域,内容超过一屏时可以正常滚动。
修改完成后即可实现NestedScrollView整体位于上方ConstraintLayout下方的布局效果。
内容的提问来源于stack exchange,提问作者cheese steak jimmy's
相关产品推荐
相关产品推荐

