【问题标题】:ConstraintLayout: Unable to Wrap_Content and Stack RecyclerView ItemsConstraintLayout:无法 Wrap_Content 和堆栈 RecyclerView 项
【发布时间】:2018-07-26 00:14:15
【问题描述】:

我有一个 RecyclerView 我正在尝试用 CardView 对象填充。每个的根视图是 ConstraintLayout。

我希望我的 Recyclerview 中的卡片每张占据 25% 的垂直屏幕空间并堆叠起来。我使用了各种准则来设置这些约束。

我注意到,虽然我可以查看 CardView,但卡片之间有足够的空间。当我希望 CardView 相互堆叠时,RecyclerView 似乎正在填充 match_parent 高度的视图:

回收站视图:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view_ingredients"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

</android.support.v7.widget.RecyclerView>

个人卡片视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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.support.constraint.Guideline
    android:id="@+id/guideline_zero"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_constraintGuide_percent=".025" />

<android.support.constraint.Guideline

    android:id="@+id/guideline_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.975" />

<android.support.constraint.Guideline
    android:id="@+id/guideline_two"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.025" />

<android.support.constraint.Guideline
    android:id="@+id/guideline_four"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.25" />

<android.support.v7.widget.CardView
    android:id="@+id/recipe_cardview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintEnd_toStartOf="@+id/guideline_one"
    app:layout_constraintStart_toStartOf="@+id/guideline_zero"
    app:layout_constraintTop_toTopOf="@+id/guideline_two"
    app:layout_constraintBottom_toTopOf="@+id/guideline_four">

    <TextView
        android:id="@+id/id_of_recipe_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: java android xml android-recyclerview android-constraintlayout


    【解决方案1】:

    ConstraintLayout 决定了每个 RecyclerView 项目的高度。由于ConstraintLayout 的高度是match_parent,每个项目将占据整个屏幕。在ConstraintLayout 内,CardView 将占 25%。剩下的 75% 将是空白的。这就是您看到报告内容的原因。

    因此,您需要一种方法使ConstraintLayout 成为屏幕高度的 25%。不幸的是,除了通过代码之外,没有办法直接为RecyclerView 的孩子指定 25% 的高度。 (这不完全正确:例如,如果您在 100dp 定义了 RecyclerVIew,则可以将 ConstraintLayout 定义为 25dp 并完成它。我假设您不想要在代码中指定准确的高度。)

    要达到 25% 的高度,您需要确定 RecyclerView 的高度 布局后 并在适配器的 onCreateViewHolder() 中设置 ConstraintLayout 的高度RecyclerView.

    【讨论】:

    • 我认为 ConstraintLayout 有一个属性,但这似乎是一个限制。我不能将我的指南用于根 ConstraintLayout?
    • @tccpg288 指南只能由指南的兄弟姐妹引用。指南无法到达其ConstraintLayout 之外以更改布局的大小。
    • 知道了,我认为这是对 ConstraintLayout 的某种限制,您同意吗?它本质上没有 wrap_content 功能,当我将 wrap_content 应用于 RecyclerView 项目的 Constraint_Layout 时,它会最小化我的所有视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多