android - 约束布局 : Create a button that fills space after a RecyclerView

标签 android android-layout android-recyclerview android-constraintlayout recyclerview-layout

我在 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/

相关文章:

android - 无尽的 RecyclerView 与 Asynctask

android - ORMLite 中的集合

android - 应用程序运行时如何在android中以编程方式关闭通知?

android - 找不到 toLeftOf 资源

java - 只有部分布局参数有效

android - 不能对 Switch 组件使用 setOnCheckedChangeListener

android - RecyclerView 与 ListView

Android RecyclerView : Change layout file LIST to GRID onOptionItemSelected

javascript - 如何在 Titanium Appcelerator 应用程序中保存持久值

java - 具有相同 Fragment 的 Android 选项卡