如果我用1.0.2,3张图片的宽度是平均的,高度是我设置的radio计算出来的。如果我使用 1.1.0,它们的高度是 0dp
并且我什么也看不到,除非我设置
android:layout_height="match_parent"
在根 ConstraintLayout
中。
这是一个错误吗?这是我的代码:
<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>
最佳答案
根据 the updated document , 布局行为在 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”
因此,在新版本中,您的 XML 中的这一行将生效:
android:layout_height="0dp"
您可以通过以下方式解决问题:
android:layout_height="0dp"
app:layout_constrainedHeight="true"
写在this answer .
更新:
我误解了这个问题。作为KongDa评论,问题没有解决:
app:layout_constrainedHeight="true"
问题已解决:
app:layout_constraintWidth_percent="0.333"
在一个最小的示例应用程序中,我检查了它的行为如下。
第一步:ConstraintLayout 1.0.2
高度不为零。
第二步:ConstraintLayout 1.1.0
高度变为零。
第三步:ConstraintLayout 1.1.0
问题已通过 app:layout_constraintWidth_percent="0.333"
解决:
因此,布局 XML 是:
<?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>
关于android - ConstraintLayout 1.1.0 与 1.0.2 不同,这是一个错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49912075/