2017-09-18 27 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