ConstraintLayout 1.1.0与1.0.2不同,这是一个错误吗? [英] ConstraintLayout 1.1.0 different from 1.0.2, is it a bug?

查看:70
本文介绍了ConstraintLayout 1.1.0与1.0.2不同,这是一个错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用1.0.2,则3张图片的宽度是平均值,它们的高度由我设置的收音机计算.如果使用1.1.0,则它们的高度为0dp,除非设置为
,否则我什么也看不到. android:layout_height="match_parent"
在根ConstraintLayout中.

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>

推荐答案

根据 ConstraintLayout 1.1.0 :

WRAP_CONTENT:强制执行约束(在1.1中添加)
如果将尺寸设置为WRAP_CONTENT,则在1.1之前的版本中,它们将被视为文字尺寸-意味着,约束将不会限制结果尺寸.通常,这足够了(并且更快),但在某些情况下,您可能想使用WRAP_CONTENT,但仍要强制执行约束以限制结果尺寸.在这种情况下,您可以添加相应的属性之一:

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
  • app:layout_constrainedWidth="true|false"
  • app:layout_constrainedHeight="true|false"

因此,在新版本中,XML中的这一行开始生效:

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"

此答案中所述.

我误解了这个问题.正如 KongDa 所评论的那样,该问题无法通过以下方式解决:

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.

高度不为零.

高度变为零.

问题已通过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>

这篇关于ConstraintLayout 1.1.0与1.0.2不同,这是一个错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆