问题
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>
回答1:
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.
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>
来源:https://stackoverflow.com/questions/49912075/constraintlayout-1-1-0-different-from-1-0-2-is-it-a-bug