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 setandroid: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>
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).
To define a view's position in ConstraintLayout , you must add at least one horizontal and one vertical constraint for the view.
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.
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.
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 toWRAP_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 useWRAP_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.
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.
The height is not zero.
The height becomes zero.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With