约束布局中的动态视图

Dynamic views in Constraint Layout

我有一个以 CardView 作为项目的 RecyclerView。

下面是我的主要布局 activity。

<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"
    tools:context="com.example.personal.newz.ui.MainActivity">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/rv"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"></android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

下面是我的回收站视图项目的布局

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_margin="16dp"
    card_view:cardCornerRadius="4dp"
    >

    <android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.constraint.Guideline
            android:id="@+id/vertical_guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.6" />

        <TextView
            android:id="@+id/date_tv"
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="2 hours ago" />

        <TextView
            android:id="@+id/source_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:textStyle="bold"
            app:layout_constraintLeft_toRightOf="@id/date_tv"
            app:layout_constraintTop_toTopOf="@id/date_tv"
            tools:text="BBC News" />

        <TextView
            android:id="@+id/headline_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="32dp"
            android:maxLines="2"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/vertical_guideline"
            app:layout_constraintTop_toBottomOf="@id/date_tv"
            tools:text="Apple admits slowing down older iphones" />

        <TextView
            android:id="@+id/description_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:maxLines="4"
            app:layout_constraintLeft_toLeftOf="@id/date_tv"
            app:layout_constraintRight_toLeftOf="@id/vertical_guideline"
            app:layout_constraintTop_toBottomOf="@id/headline_tv"
            tools:text="Customers have long suspected iPhones slow down over time. Now, Apple has confirmed some models do." />

        <ImageView
            android:id="@+id/article_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            app:layout_constraintHorizontal_bias=".7"
            app:layout_constraintLeft_toRightOf="@id/vertical_guideline"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/source_tv"
            tools:src="@drawable/placeholder" />

        <ImageView
            android:id="@+id/bookmark_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:src="@drawable/bookmark"
            app:layout_constraintEnd_toStartOf="@+id/share_image"
            app:layout_constraintStart_toEndOf="@+id/vertical_guideline"
            app:layout_constraintTop_toBottomOf="@id/article_image"
            app:layout_constraintHorizontal_chainStyle="packed"/>

        <ImageView
            android:id="@+id/share_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/share_icon"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/bookmark_image"
            app:layout_constraintTop_toBottomOf="@id/article_image" />


    </android.support.constraint.ConstraintLayout>

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

现在,当我第一次启动我的应用程序时,单个 cardview 的宽度与父级不匹配,并且它们的宽度表现得好像是包装内容。几秒钟后,所有卡片视图的宽度都会自行调整以匹配父视图。

ConstraintLayout 实际上不支持 match_parent 因为它的 children。如果您尝试使用 match_parent 有时 它会起作用,有时它不会。 Android Studio 在 match_parent 方面也有点奇怪,有时会允许它,有时会自动将其替换为与您 运行 最后一个模拟器相匹配的硬编码值。无论如何,不​​要将 match_parent 用于 ConstraintLayout 的 children。

代替android:layout_width="match_parent",使用:

android:layout_width="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"

代替android:layout_height="match_parent",使用:

android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

当我对您的 RecyclerView 标签进行这些更改后,问题就消失了。