2017-03-22 43 views
2

我會讓有70%的寬度的視圖,並使用約束佈局對齊到其父的權利,遵循約束佈局有個工作不正常

<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" 
    > 
    <android.support.constraint.Guideline 
     android:id="@+id/guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.3"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:text="Hello text" 
     app:layout_constraintLeft_toRightOf="@+id/guideline" 
     app:layout_constraintRight_toRightOf="parent"/> 

</android.support.constraint.ConstraintLayout> 

TextView的始終佔據全父寬度。任何想法我做錯了什麼?

回答

6

兩個小但重要的變化:

  1. TextView的寬度應0dp即匹配約束和不匹配父
  2. 指引方向應垂直不是水平

下面的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    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/guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.3" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Hello text" 
     app:layout_constraintLeft_toRightOf="@id/guideline" 
     app:layout_constraintRight_toRightOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

輸出:

enter image description here

另外請注意,我改變了ConstraintLayout高度match_parent,這樣的方針是在輸出可見。您可以將其更改回wrap_content。