【问题标题】:ConstraintLayout from bottom to top从下到上的约束布局
【发布时间】:2021-01-06 23:02:05
【问题描述】:

我有这样的布局:

<androidx.constraintlayout.widget.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">

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/colorPrimary"
    android:minHeight="300dp"
    app:layout_constraintBottom_toTopOf="@+id/recyclerView"
    app:layout_constraintHeight_min="300dp"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

FragmentContainerViewRecyclerView 具有动态大小,FragmentContainerView 不应小于 300dp 并适合所有可用的 spase。 RecyclerView 应该在底部,可以包含 1 到 20 个项目。当前解决方案仅适用于RecyclerView 包含少量项目的情况,但对于其他情况,它与FragmentContainerView 重叠。 是否可以将RecyclerView 与底部对齐但限制最大尺寸以使可用空间 = 300dp 在顶部?

【问题讨论】:

  • 你能添加一些截图发生了什么吗?

标签: android android-constraintlayout


【解决方案1】:

我认为你需要这样做:

  • app:layout_constraintTop_toBottomOf="@id/container" 添加到RecyclerView
  • FragmentContainerView 中删除app:layout_constraintBottom_toTopOf="@+id/recyclerView"

这将使FragmentContainerView 的高度不限于RecyclerView 的高度.. 并使RecyclerView 的高度限制在FragmentContainerView 的底部和父级的底部之间。

【讨论】:

  • 在我的情况下 FragmentContainerView 也有很大的高度,所以它与 recyclerview 重叠 :( 但我有你的想法,会尝试做类似的事情。谢谢
  • @Siarhei 如果您将android:maxHeight="400dp" 设置为FragmentContainerView 会怎样......但要小心小屏幕设备
  • 是的,为每个设备尺寸设置具有特定值的约束 maxHeight 似乎是更好的解决方案。谢谢
【解决方案2】:

尝试在 RecyclerView 中添加app:layout_constraintTop_toBottomOf="@id/container"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2022-11-02
    • 2019-03-29
    • 1970-01-01
    相关资源
    最近更新 更多