文本视图未与约束布局中的准则对齐

Text view not aligning to the guideline in constraint layout

我有以下约束布局:

`<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:layout_margin="16dp">

    <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="wrap_content"
            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_constraintTop_toBottomOf="@id/date_tv"
            app:layout_constraintRight_toLeftOf="@id/vertical_guideline"
            tools:text="Apple admits slowing down older iphones" />

    </android.support.constraint.ConstraintLayout>

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

它有以下输出:

现在我想要的是标题文本视图的宽度应仅与指南一样宽,并且当文本很长时应移至下一行。这没有发生。有人可以帮我解决这个问题吗?

尝试将宽度设置为 0dp (match_constraints),如下所示:

    <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_constraintTop_toBottomOf="@id/date_tv"
        app:layout_constraintRight_toLeftOf="@id/vertical_guideline"
        tools:text="Apple admits slowing down older iphones" />

您正在使用 android:width=wrap_content 作为 headline_tv,它优先考虑文本视图的实际宽度。使用 0dp 以匹配约束条件。