Android 为 view2 设置约束以跟随 view1 的开始。但是 view1 对其父级的开始有约束

Android Setup constraint for view2 to follow start of view1. But view1 has constraint to start of its parent

我做不到这一点。 我希望 viewB 跟随 viewA 的开始。 然后我想创建一个约束,使 space 从 viewA 的开始到它的父级。

.

我试过的代码:

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <TextView
            android:id="@+id/descriptionTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="32dp"
            android:layout_marginStart="8dp"
            android:text="Txt1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/descriptionTxt2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Txt2"
            app:layout_constraintEnd_toEndOf="@+id/descriptionTxt"
            app:layout_constraintStart_toStartOf="@+id/descriptionTxt"
            app:layout_constraintTop_toBottomOf="@+id/descriptionTxt" />

    </android.support.constraint.ConstraintLayout>

上面的代码将在预览中显示只有 viewA 会有左边距。 viewB 没有跟随 viewA 的左侧。

我正在使用 com.android.support.constraint:constraint-layout:1.0.2

try this :

<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="wrap_content">

<TextView
    android:id="@+id/descriptionTxt"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Txt1"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

<TextView
    android:id="@+id/descriptionTxt2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Txt2"
    android:layout_marginTop="8dp"
    app:layout_constraintRight_toRightOf="@+id/descriptionTxt"
    app:layout_constraintLeft_toLeftOf="@+id/descriptionTxt"
    app:layout_constraintTop_toBottomOf="@+id/descriptionTxt"
    />

</android.support.constraint.ConstraintLayout> 

不要使用match_parent。相反,开始对 match_parent 使用“0dp”并定义 left/right 或 top/bottom 父约束

这是工作代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_margin="10dp"
   xmlns:android="http://schemas.android.com/apk/res/android">

  <TextView
    android:id="@+id/descriptionTxt"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="32dp"
    android:layout_marginStart="8dp"
    android:text="Txt1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

  <TextView
    android:id="@+id/descriptionTxt2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Txt2"
    app:layout_constraintEnd_toEndOf="@+id/descriptionTxt"
    app:layout_constraintStart_toStartOf="@+id/descriptionTxt"
    app:layout_constraintTop_toBottomOf="@+id/descriptionTxt" />

</android.support.constraint.ConstraintLayout>

另外,最好使用 Guidelines 来获得统一的 left/top/right/bottom 边距。