2017-09-18 26 views
0

我无法实现这个。 我想让viewB跟随viewA的开始。 然后,我想创建一个约束,从viewA的开始到它的父节点有一个空间。Android设置约束让view2跟随视图1的开始。但是view1对其父项的开始有约束

enter image description here

代码我想:

<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:约束的布局:1.0.2

回答

3

不要使用match_parent。取而代之的是,开始使用“0dp”为match_parent和定义左/右或顶部/底部父约束

这里是工作代码:

<?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> 

而且,这是很好的使用准则有统一的左/顶/右/下边距。

+0

感谢您的解释。现在我明白了 – iori24

2

试试这个:

<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> 
+0

感谢您的帮助 – iori24