为什么ConstraintLayout 1.1.0 layout_constraintWidth_percent和layout_constraintDimensionRatio不能一起使用?

时间:2018-06-23 06:43:52

标签: android android-layout android-constraintlayout

我正在尝试在ImageView中同时使用layout_constraintDimensionRatio和layout_constraintWidth_percent属性,但是我发现它们无法一起使用。

我的xml代码是:

1。仅使用layout_constraintWidth_percent:

<?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="match_parent">
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>

其对应的UI为: layout_constraintWidth_percent_only.png

2。使用layout_constraintWidth_percent和layout_constraintDimensionRatio:

<?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="match_parent">
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintDimensionRatio="w,2:1"
        android:scaleType="fitXY"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>

其对应的UI为: layout_constraintWidth_percent_and_layout_constraintDimensionRatio.png

很明显,当我使用layout_constraintDimensionRatio时,layout_constraintWidth_percent不起作用。

我真正想要的是ImageView的宽度是其父级的50%,然后通过一起使用layout_constraintWidth_percent和layout_constraintDimensionRatio来使ImageView的高度为其宽度的50%。

1 个答案:

答案 0 :(得分:0)

设置android:layout_height="0dp"来匹配约束应有助于您获得所需的结果。

根据文档:

  

如果两个尺寸都设置为MATCH_CONSTRAINT(0dp),则也可以使用比率。在这种情况下,系统设置最大尺寸以满足所有约束并保持指定的宽高比。

<?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="match_parent">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintDimensionRatio="2:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher"/>

</android.support.constraint.ConstraintLayout>

enter image description here

编辑似乎app:layout_constraintWidth_percent本身并未向View提供实际的水平约束,因此以下行为会有所不同(首先是right水平约束集,第二个则没有):

<ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        app:layout_constraintDimensionRatio="H,2:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.5"
        app:srcCompat="@mipmap/ic_launcher" />

<ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        app:layout_constraintDimensionRatio="H,2:1"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.5"
        app:srcCompat="@mipmap/ic_launcher" />

我们将宽度限制为父级的百分比,然后通过使用H中的app:layout_constraintDimensionRatio="H,2:1"来限制宽度的高度。在第一种情况下,该视图不显示,而在第二种情况下,该视图实际上可以正确显示。看来,如果要使用app:layout_constraintWidth_percent,则需要同时添加两个水平约束,然后才能根据宽度正确约束高度。

作为该方法的替代方法,您可以使用Guideline设置为父宽度的50%来约束ImageView,然后根据比例来约束高度:

<?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="match_parent">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        app:layout_constraintDimensionRatio="H,2:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/guideline"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.5"
        app:srcCompat="@mipmap/ic_launcher" />

</android.support.constraint.ConstraintLayout>