【问题标题】:How to set recyclerview minimum height wrap_content but maximum in percent using constraint layout?如何使用约束布局设置recyclerview最小高度wrap_content但最大百分比?
【发布时间】:2021-02-27 15:08:30
【问题描述】:

我有一个 recyclerview 并且我正在使用约束布局。现在我想将高度用作 wrap_content 但想以百分比设置最大高度。我如何使用约束布局来实现这一点?

   <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:visibility="gone"

    app:layout_constraintTop_toBottomOf="@+id/search_view"

    android:layout_height="0dp"
    app:layout_constraintHeight_default="wrap"
    app:layout_constraintHeight_max="500dp"
    android:scrollbars="vertical"/>

【问题讨论】:

  • 内容大于最大高度会怎样?
  • @FrancescoBocci 然后它的工作正常包装内容
  • 在约束布局中使用指南并将单位更改为百分比并将回收者视图约束为指南

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


【解决方案1】:

由于您使用的是ConstraintLayout,因此您可以使用Guideline 来限制RecyclerView's 的高度。

<androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.85"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        app:layout_constraintTop_toBottomOf="@+id/search_view"
        app:layout_constraintBottom_toBottomOf="@id/guideline3" />

Guideline 有一个layout_constraintGuide_percent,您可以在其中为RecyclerView 指定所需的百分比。

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2015-04-20
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多