ConstraintLayout 1.1.0 different from 1.0.2, is it a bug?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 09:35:17

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.

Step 2: ConstraintLayout 1.1.0

The height becomes zero.

Step 3: ConstraintLayout 1.1.0

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

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!