【问题标题】:Constraint layout with top title, recycler view and bottom button带有顶部标题、回收站视图和底部按钮的约束布局
【发布时间】:2022-02-13 00:27:40
【问题描述】:

我正在尝试实现一个布局,其中将包含顶部标题、回收器视图和底部按钮。 看起来很简单,但我需要为约束布局设置一个 wrap_content 高度,以使其根据内容(顶部标题、底部按钮和回收器视图)进行扩展。我已经找到的所有示例都具有 match_parent 值。另外,我需要保留屏幕上的标题和底部按钮,那里有很多回收视图项目。反过来回收视图必须填满所有可用的地方,并且不要重叠标题和底部按钮。

所以,布局要求:

  1. 标题始终在屏幕上
  2. 底部按钮始终在屏幕上
  3. 约束布局必须根据回收站视图内容进行扩展(下图)

概念:

我现在的布局

 <?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="wrap_content">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title"
        android:background="@color/yellow"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="16dp"
        android:textStyle="bold" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvList"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/constraint_parent"
        app:layout_constraintBottom_toTopOf="@id/btnSave"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvTitle"
        tools:itemCount="50" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnSave"
        android:layout_width="@dimen/constraint_parent"
        android:layout_height="wrap_content"
        android:text="Save"
        android:textSize="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

结果 - 不显示回收站视图

也许还有另一种方法可以做到这一点,但我花了很多时间寻找解决方案。我相信解决方案应该很简单,但我还没有找到一个。 DialogFragment 不适合此处,因为当应用处于纵向时,此屏幕需要处于全屏模式。

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    只需将底部按钮 (btnSave) 约束为 - app:layout_constraintTop_toBottomOf="@id/rvList"

    同样不要固定recyclerView的高度。保留为 wrap_content

    【讨论】:

    • 不幸的是,它没有帮助。当我为约束布局设置 wrap_content 时,我仍然遇到未显示的回收器视图的问题。
    【解决方案2】:

    尝试在您的ContraintLayout 上将android:layout_height 设置为match_parent。

    【讨论】:

    • 是的,它有帮助,但它不会使布局按内容高度缩放
    猜你喜欢
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多