android - ConstraintLayout 1.1.0 与 1.0.2 不同,这是一个错误吗?

标签 android android-layout android-constraintlayout

如果我用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 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”

因此,在新版本中,您的 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

高度不为零。

enter image description here

第二步:ConstraintLayout 1.1.0

高度变为零。

enter image description here

第三步:ConstraintLayout 1.1.0

问题已通过 app:layout_constraintWidth_percent="0.333" 解决:

enter image description here

因此,布局 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/

相关文章:

android - 关于跨平台项目中 Web 语言互操作性的问题

android - 如何使 Tab Widgets 中的文本占两行?

android - 无法访问 fragment 内的 Textview

android - 具有 ConstraintLayout 和填充的 LinearLayout

android - 如何将文本从 Activity(从 gridView 中选择的项目)发送到 fragment 并将文本发送到按钮

android - 网页上的图像在 Android 8/9 上的 Chrome 78 上不可见(但在 Chrome 76 和 Android 10 上工作)——可能是什么问题?

android - <ConstraintLayout/> 中 <TextView/> 下方的边距不起作用

Android水平 ScrollView 不滚动

android - Facebook 不显示好友列表

android - 以编程方式在 API 级别 7 中设置 View 位置