如何在ConstraintLayout中wrap_content或“填充可用空间”

问题描述 投票:2回答:2

我有这个布局:

<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 >

此recyclerview将添加到“id / content”framelayout

<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" />

它具有令人满意的效果,即回收视图位于屏幕的底部。

enter image description here

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

android android-layout android-recyclerview android-constraintlayout
2个回答
4
投票

啊!我忘了在顶部添加垂直约束。将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" />

1
投票

我认为问题来自布局的层次结构。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.