Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout 1.1.0 different from 1.0.2, is it a bug?

Tags:

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>
like image 211
KongDa Avatar asked Apr 19 '18 03:04

KongDa


People also ask

Is ConstraintLayout a ViewGroup?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).

Which is at least required for ConstraintLayout?

To define a view's position in ConstraintLayout , you must add at least one horizontal and one vertical constraint for the view.

What is Androidx ConstraintLayout widget ConstraintLayout?

GitHub - androidx/constraintlayout: ConstraintLayout is an Android layout component which allows you to position and size widgets in a flexible way. Skip to content Toggle navigation. Product.

Should you use ConstraintLayout?

Advantages of using ConstraintLayout in AndroidIt helps to improve the UI performance over other layouts. With the help of ConstraintLayout, we can control the group of widgets through a single line of code. With the help of ConstraintLayout, we can easily add animations to the UI components which we used in our app.


1 Answers

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>
like image 124
qtmfld Avatar answered Sep 28 '22 06:09

qtmfld