2017-05-20 104 views
2

我用constraintLayout和layout_constraintDimensionRatio = 「1:1」 (寬度warp_content,高度是0dp(match_constraint))ConstraintLayout layout_constraintDimensionRatio不工作

結果,我期望的寬度和高度爲1:1,但不工作。

出了什麼問題?

我附上了代碼和截圖。

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/t1" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:background="@android:color/holo_blue_bright" 
     android:gravity="center" 
     android:text="Hello World!11" 
     app:layout_constraintDimensionRatio="1:1" /> 

</android.support.constraint.ConstraintLayout> 

screenshot

我引述有關Constraintlayout Android開發者網站。 https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints

「比:: 您還可以定義一個小部件的一個尺寸爲另一種的比例。爲了做到這一點,你需要至少有一個約束的尺寸設置爲0dp(即MATCH_CONSTRAINT),並且屬性layout_constraintDimentionRatio設置成給定的比例,例如:

 <Button android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       app:layout_constraintDimensionRatio="1:1" /> 

將設置按鈕的高度是相同的作爲其寬度「。

- >但我沒有工作。

+0

ConstrainLayout的版本是什麼? – Shailesh

+0

嗨Shailesh。我用1.0.2 – pistolcaffe

+0

'match_parent'不支持。你需要設置'0dp',以便'ConstraintLayout'相應地設置它。檢查這個http://stackoverflow.com/questions/37603751/set-width-to-match-constraints-in-constraintlayout?rq=1 – Shailesh

回答

8

您忘記添加約束

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

    <TextView 
     android:id="@+id/t1" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:background="@android:color/holo_blue_bright" 
     android:gravity="center" 
     android:text="Hello World!11" 
     app:layout_constraintDimensionRatio="1" /> 

</android.support.constraint.ConstraintLayout> 

0dp僅適用於ConstraintLayout子視圖。 任何視圖都應該應用其父項的屬性規則。

+0

我忘了將寬度設置爲0dp。 但是,我不知道爲什麼Android開發人員網站上的示例顯示它僅在插入時才起作用。 – pistolcaffe

-1

請注意最後一行,在邊緣周圍添加約束使約束起作用。

您還可以使用Studio中的設計視圖,並在對象之間拖放約束。

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Available chats" /> 

<ListView 
    android:id="@+id/listChats" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_constraintTop_toBottomOf="@+id/textView"/> 
+0

你的答案如何解決問題?源代碼的最後一行只表示一個約束,但是應該有更多的約束來使比例起作用! –