繁体   English   中英

如何让recyclelerView在android约束布局中占用最多一半的屏幕(最大值)?

[英]How can I make a recyclerView to take up to half the screen(max) in android constraint layout?

我有一个包含2个视图的屏幕,一个地图(顶部)和一个回收者(底部)规则很简单。

允许再循环视图延伸到屏幕中间,如果需要更多空间,那么它应该滚动,地图将占据剩余的空间。当然,如果回收器具有较少的元素,那么它应该收缩,留下更多地图的空间..

我试图使用约束布局来实现这一点,我也试图避免涉及计算的解决方案。

查看下面的图片,了解我想要实现的目标的更多信息: 在此输入图像描述

这是我的代码

<?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/white"
    tools:context="com.qmic.itraffic.ui.activities.crowdsourcing.CrowdSourceReportingDetailsActivity">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/halfScreenGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/categories"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@color/manatee"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/categories"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:clipToPadding="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/halfScreenGuideline" />



</androidx.constraintlayout.widget.ConstraintLayout>

我的问题是我可以仅使用xml(约束布局)来实现此行为吗?或者我需要进行计算?

您可以在recyclerview xml中尝试以下代码吗?

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"

更改高度以包装内容并添加第二行。 这样,recyclerview的高度应与其内容相同,但绝不会超出指南/约束。

Akhil Soman的回答是正确的但却缺少一件事

app:layout_constraintVertical_bias="1.0"

没有它,RecyclerView浮动在屏幕底部之上,留下空白空间。

这里的参考是完整的答案

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/categories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:clipToPadding="false"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/halfScreenGuideline"
        app:layout_constraintVertical_bias="1.0" />

Jaymin的建议似乎很有意思并且可以解决这个问题,但我还没有应用它,因为我已经使用过Akil解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM