5

If I use 1.0.2, the 3 images' width is average, and the height of them is computed by the radio which I set. If I use 1.1.0, the height of them is 0dp and I can't see nothing, unless I set
android:layout_height="match_parent"
in the root ConstraintLayout.

Is it a bug? Here is my code:

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

    <ImageView
        android:id="@+id/iv0"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#FF0000"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/iv1"
        app:layout_constraintDimensionRatio="2:1"/>

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#00FF00"
        app:layout_constraintDimensionRatio="2:1"
        app:layout_constraintLeft_toRightOf="@id/iv0"
        app:layout_constraintRight_toLeftOf="@+id/iv2"/>

    <ImageView
        android:id="@+id/iv2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#0000FF"
        app:layout_constraintDimensionRatio="2:1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/iv1"/>

</android.support.constraint.ConstraintLayout>
qtmfld
  • 2,916
  • 2
  • 21
  • 36
KongDa
  • 51
  • 5

1 Answers1

4

According to the updated document, the layout behavior has changed in ConstraintLayout 1.1.0:

WRAP_CONTENT : enforcing constraints (Added in 1.1)
If a dimension is set to WRAP_CONTENT, in versions before 1.1 they will be treated as a literal dimension -- meaning, constraints will not limit the resulting dimension. While in general this is enough (and faster), in some situations, you might want to use WRAP_CONTENT, yet keep enforcing constraints to limit the resulting dimension. In that case, you can add one of the corresponding attribute:

  • app:layout_constrainedWidth=”true|false”
  • app:layout_constrainedHeight=”true|false”

So, in the new version, this line in your XML is taking effect:

android:layout_height="0dp"

You can fix the problem with:

android:layout_height="0dp"
app:layout_constrainedHeight="true"

as written in this answer.


Updated:

I misunderstood the question. As KongDa commented, the problem is not fixed with:

app:layout_constrainedHeight="true"

The problem is fixed with:

app:layout_constraintWidth_percent="0.333" 

In a minimal sample app, I checked its behavior as follows.

Step 1: ConstraintLayout 1.0.2

The height is not zero.

enter image description here

Step 2: ConstraintLayout 1.1.0

The height becomes zero.

enter image description here

Step 3: ConstraintLayout 1.1.0

The problem is fixed with app:layout_constraintWidth_percent="0.333":

enter image description here

So, the layout XML is:

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

    <ImageView
        android:id="@+id/iv0"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#FF0000"
        app:layout_constraintDimensionRatio="2:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/iv1"
        app:layout_constraintWidth_percent="0.333" />

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#00FF00"
        app:layout_constraintDimensionRatio="2:1"
        app:layout_constraintLeft_toRightOf="@id/iv0"
        app:layout_constraintRight_toLeftOf="@+id/iv2"
        app:layout_constraintWidth_percent="0.333" />

    <ImageView
        android:id="@+id/iv2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#0000FF"
        app:layout_constraintDimensionRatio="2:1"
        app:layout_constraintLeft_toRightOf="@id/iv1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintWidth_percent="0.333" />

</android.support.constraint.ConstraintLayout>
qtmfld
  • 2,916
  • 2
  • 21
  • 36
  • Thank you!! But your fix method seems don't effect in my code, and I set `app:layout_constraintHeight_default="percent"` to solved my problem. Thank you ! – KongDa Apr 23 '18 at 06:09
  • @KongDa I'm sorry, I misunderstood your question. I checked the behavior step by step, and updated my answer. – qtmfld Apr 23 '18 at 08:45
  • Yes, Completely correct, Thanks for your perfect answer! – KongDa Apr 23 '18 at 12:11