为简单起见,我有一个ConstraintLayout
,有2个孩子:RecyclerView
和Button
。
我希望RecyclerView
从父级的顶部开始显示。
如果RecyclerView
中仅显示少量项目,则应将它们包装起来。像这样:
但是,如果RecyclerView
有足够的项目可以显示到屏幕末尾,则应改为Button
的顶部,而不是其父项的底部。
像这样:
为达到此效果,我尝试了以下组合:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/my_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
当RecyclerView
仅显示少量项目时,此解决方案非常好。否则,如果必须扩展到屏幕之外,它将进入Button
后面。
将上面的xml修改为(注意app:layout_constraintBottom_toTopOf="@id/my_button"
的行RecyclerView
):
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/my_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@id/my_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我得到的结果是:
也就是说,RecyclerView
位于父级顶部和按钮顶部之间的中心。
如果RecyclerView
有很多项目,则可以,但是如果项目数量很少,则不能。
问题:
是否可以使我的RecyclerView
像这样:
不管项目的数量如何,都应使其最高位置位于其父项的顶部。
如果物品很少,则将内容包裹到最后一个物品的底部。
如果有很多项目,请扩展到Button
的顶部,而不要扩展到parent
的底部
?
P.S。该解决方案应仅用作父级ConstraintLayout
。
答案 0 :(得分:0)
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/my_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/my_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
答案 1 :(得分:0)
您可以使用值为app:layout_constraintHorizontal_bias
的{{1}}属性,将0
与其顶部约束对齐(RecyclerView
将与底部约束对齐)。为了使用此偏差,需要同时设置顶部和底部约束。另一方面,如果未设置底部约束,那么您将无法防止1
与按钮重叠。这就是为什么设置底部约束并保留RecyclerView
以确保在其高度设置为app:layout_constrainedHeight="true"
时强制执行View's
约束的重要性。
wrap_content
的顶部约束也不正确,因为其顶部应约束在父对象的顶部而不是底部。
RecyclerView