android 具有顶部标题、回收器视图和底部按钮的约束布局

r3i60tvu  于 2022-12-21  发布在  Android
关注(0)|答案(3)|浏览(116)

我试图实现一个布局,这将包含顶部标题,回收视图和底部按钮。看起来很简单,但我需要有一个约束布局的wrap_content高度,使其根据内容(顶部标题,底部按钮和回收视图。所有的例子,我已经找到了match_parent值。此外,我需要保持标题和底部按钮在屏幕上,那里有很多回收视图项目。反过来回收视图必须填补所有可用的地方,不要重叠标题和底部按钮。
因此,布置要求:
1.标题始终显示在屏幕上
1.底部按钮始终在屏幕上
1.约束布局必须可根据回收器视图内容进行缩放(下图)
概念:

我现在拥有的布局

<?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不适合这里,因为当应用处于纵向时,这个屏幕需要处于全屏模式。

bpsygsoo

bpsygsoo1#

仅将底部按钮(btnSave)约束为-app:layout_constraintTop_toBottomOf="@id/rvList"
另外,不要固定recyclerView的高度,将其保留为wrap_content

sy5wg1nm

sy5wg1nm2#

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

xxhby3vn

xxhby3vn3#

尝试设置android:layout_height=“0dp”和
应用程序:循环视图的布局约束高度=“true”

相关问题