在ConstraintLayout中,我有两个垂直排列在彼此顶部的RecEconerView view_aview_b. 宽度:简单,Match_Parent.我的问题是身高.

最下面的view_b个应该包装它的内容,并扩大到三分之一的空间. 前view_a名应该会填补剩余的空间.

例如,如果view_b只包含1项,我希望它只填充高度的1/10,view_a应该填充其余的9/10.但当view_b包含1000项时,它应该填充1/3(并滚动以显示隐藏的项),将2/3留给view_a.

我try 了以下几种方法.然而,这只实现了固定的2:1的视图比例. 如果view_b小于1/3,它仍然填充底部的整个1/3,并且不对其内容进行换行.

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/view_a"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layoutManager="LinearLayoutManager"
        app:layout_constraintBottom_toTopOf="@id/view_b"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="2" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/view_b"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layoutManager="LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view_a"
        app:layout_constraintVertical_weight="1" />

</androidx.constraintlayout.widget.ConstraintLayout>

我还试着把app:layout_constraintHeight_default="wrap"加到view_b. 但随后它总是包装其内容,并扩展到超过三分之一,变得太大.

推荐答案

这可以通过在view_b (RecyclerView)中设置以下属性来实现:

app:layout_constraintHeight_max="wrap"
app:layout_constraintHeight_percent="0.3"
app:layout_constrainedHeight="true"

根据上述属性,如果view_b (RecyclerView)包含来自1/3屏幕的较小数据量,它将换行到其内容,但如果包含大量数据并超过1/3屏幕,它将获得的最大高度为屏幕的1/3,始终将剩余空间保留为view_a (RecyclerView).

最终的XML应该如下所示:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/view_a"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/view_b"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/view_b"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_max="wrap"
        app:layout_constraintHeight_percent="0.3"
        app:layout_constrainedHeight="true"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Android相关问答推荐

在Android Studio中陷入了不兼容的Gradle版本的循环

以编程方式更改Android应用程序上的文件许可

更改Jetpack Compose中选定的选项卡底线 colored颜色

在Jetpack Compose中将导航绘图显示在顶部栏下方、底部栏上方

返回并再次打开webview时webview无法打开相机

使用lazyColumn迁移paging3的旧代码

升级到 Jetpack Compose 物料 list 2023.08.00 需要我将 targetSdk 更改为 34

从 HiltViewModel @Injection 访问 Application()

如何在 Jetpack Compose 中向图像视图添加对角色带?

在事件中使用 Context/Toast 时不需要的重组 - Jetpack Compose

如何像 XML 一样在 Compose Android Studio 中折叠/展开小部件代码区域/区域

如何在jetpack compose中通过lamda返回columnScope/RowScope

观察软键盘可见性,打开/关闭 Jetpack Compose

缺少类 com.google.android.datatransport.runtime.ForcedSender

喷气背包组成影子奇怪的行为

如何在 Android 应用中录制短视频?

运行一次 kotlin 流,但在下游收到两次

房间实时数据:启动时崩溃

compose :为什么以记住启动的列表触发方式与快照不同

(Android) 如何在一对多关系中将子 ID 与父级匹配