【问题标题】:How to wrap_content or "fill available space" in ConstraintLayout如何在 ConstraintLayout 中包装内容或“填充可用空间”
【发布时间】:2018-09-07 21:40:58
【问题描述】:

我有这个布局:

<android.support.constraint.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="match_parent">
    ... 
    <FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout >

这个rec​​yclerview被添加到“id/content”框架布局中

<android.support.v7.widget.RecyclerView 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_gravity="bottom"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layoutManager="LinearLayoutManager" />

recyclerview 放在屏幕底部,效果理想。

当recyclerview中有很多viewholder时,问题就出现了。我想在顶部留出一些空间以仍然看到地图(200dp 边距)。我尝试了很多方法,似乎无法找到一个优雅的解决方案。基本上我想要的是 recyclerview 将 wrap_content 直到内容太大。如果内容太大,我希望 recyclerview 扩展以填充它可以填充的空间,同时在顶部保留 200dp。在 iOS 中,这可以使用 >= 200 约束。这在安卓上可行吗?怎么样?

【问题讨论】:

  • 那么地图View在FrameLayout之外?而且 200dp 的边距不起作用?
  • 看看BottomSheetBehavior,看看它是否能满足您的需求。

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


【解决方案1】:

啊!我忘了在顶部添加一个垂直约束。将 FrameLayout 更改为此就可以了。

    <FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="200dp"
    app:layout_constraintTop_toBottomOf="@id/appBarLayout"
    app:layout_constraintBottom_toBottomOf="parent" />

【讨论】:

  • 不使用CoordinatorLayout的时候为什么需要AppBarLayout,可以直接使用RecyclerView的情况下为什么还要在FrameLayout中添加RecyclerView?
  • 关于使用 CoordinatorLayout 的好提示。至于框架布局,我在其他地方重用了那个recyclerview,所以没有直接添加它。
【解决方案2】:

我认为问题出在布局的层次结构上。

将RecyclerView添加到android.R.id.content将其添加到ConstraintLayout的同一级别,也许如果您将其直接添加到ConstraintLayout并对其施加一些约束,您可以实现您想要的。

虽然,BottomSheetBehavior 可能是您想要模仿的组件?

【讨论】:

  • BottomSheetBehavior 的好提示。我不知道它存在。
猜你喜欢
  • 2020-06-16
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 2011-03-23
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多