【问题标题】:ConstraintLayout - how to wrap over RecyclerViewConstraintLayout - 如何包裹 RecyclerView
【发布时间】:2020-05-17 14:18:41
【问题描述】:

我有一个 ConstraintLayout,其中有一个 appBar(包含一个工具栏)、两个片段(每个片段包含一个 RecyclerView)和一个底部的按钮。

我想要做的是将布局的高度包裹在 RecyclerViews 的大小之上。如下图所示,无论列表大小如何,每个 Recyclerview 都采用相同的高度。

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/template_grey_light"
    tools:context=".presentation.ui.activity.MainActivity">

    <include
        android:id="@+id/AppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/fragmentTop"
        app:layout_constraintVertical_chainStyle="packed"
        layout="@layout/app_bar" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentTop"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/AppBar"
        app:layout_constraintBottom_toTopOf="@+id/fragmentBottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentBottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/fragmentTop"
        app:layout_constraintBottom_toTopOf="@+id/shuffleButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <Button
        android:id="@+id/shuffleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@color/template_love_red"
        android:text="Go!"
        android:layout_marginBottom="20dp"
        app:layout_constraintTop_toBottomOf="@+id/fragmentBottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我知道我在高度属性上有 0dp,这使它的高度相等,但是如果我不这样做,所有东西都会在顶部重叠。关于如何使它工作的任何想法?

【问题讨论】:

  • 你想让碎片高度和里面的recyclerview一样吗?
  • 是的,那将是理想的。现在他们有一个固定的高度
  • FragmentContainerView 标签中使用android:layout_height="wrap_content"
  • 谢谢,这行得通。现在的问题是所有视图都居中,在工具栏和顶部之间留下了空隙。底部同理,按钮与屏幕底部有空隙
  • 检查你的recyclerview是否有边距或内边距。还要检查片段内布局中的重力

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


【解决方案1】:

在您的FragmentContainerView 标签中使用android:layout_height="wrap_content"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2011-01-24
    • 2019-04-26
    • 2017-03-29
    • 2017-07-18
    • 2023-04-05
    相关资源
    最近更新 更多