Android 文本视图未与约束布局中的准则对齐
我有以下约束布局:Android 文本视图未与约束布局中的准则对齐,android,android-constraintlayout,Android,Android Constraintlayout,我有以下约束布局: `<?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.an
`<?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\u 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\u内容,它优先考虑文本视图的实际宽度。使用0dp使其匹配约束。谢谢,它解决了问题。但是你能解释一下为什么这个解决方案有效吗?提前谢谢