我在 ConstraintLayout 中有一个水平 RecyclerView。我想在 RecyclerView 的末尾放置一个按钮。当回收器为空时,按钮应扩展到所有可用空间。当元素被添加到回收器时,按钮应该挤压到最小。
ConstraintLayout 1.1.0 中有一些新功能,例如 layout_constraintWidth_min
,但我无法使其正常工作。它应该按照我想要的方式工作吗?
以下是相关属性,不包括高度:
<android.support.v7.widget.RecyclerView
android:id="@+id/horizontal_recycler"
android:layout_width="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/add">
</android.support.v7.widget.RecyclerView>
和
<Button
android:id="@+id/add"
android:layout_width="0dp"
app:layout_constraintWidth_min="80dp"
app:layout_constraintStart_toEndOf="@+id/horizontal_recycler"
app:layout_constraintEnd_toEndOf="parent">
</Button>
最佳答案
这是我的解决方案。
<android.support.constraint.ConstraintLayout
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="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginEnd="80dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="ADD"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/recyclerView"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
请注意,为了防止 recyclerView 挤压按钮,回收器上的边距端应等于按钮加上您认为合适的任何填充和边距。
关于android - 约束布局 : Create a button that fills space after a RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47332302/