ConstraintLayout 问题(最大宽度、尺寸比和链样式)

xca*_*aei 2 android android-layout android-constraintlayout

我对 ConstraintLayout 和特定视图有疑问。

所需内容:3 个 6:5 比例的图像视图,第一个较大,另外两个垂直堆叠在其右侧。整个视图的最大宽度为 414dp。

当尝试将水平链样式设置为 Packed 时,视图根本不会显示超过 414dp。在该限制下或除了包装之外的链条样式不存在任何问题。

这里有

我错过了什么吗?

Ben*_* P. 5

为了实现你想要的,我相信你必须将一个嵌套ConstraintLayout在另一个中ConstraintLayout

问题是你不能在同一个视图上同时定义app:layout_constraintWidth_percent和;layout_constraintWidth_max其中每一个都是如何将视图集约束为“匹配约束”的一个选项。

MATCH_CONSTRAINT 维度(1.1 中添加)

当尺寸设置为 时MATCH_CONSTRAINT,默认行为是使结果尺寸占据所有可用空间。有几个附加修饰符可用:

  • layout_constraintWidth_minlayout_constraintHeight_min:将设置该尺寸的最小尺寸
  • layout_constraintWidth_maxlayout_constraintHeight_max:将设置该尺寸的最大尺寸
  • layout_constraintWidth_percentand layout_constraintHeight_percent: 将此尺寸的大小设置为父尺寸的百分比

(来自https://developer.android.com/reference/android/support/constraint/ConstraintLayout

您可以通过让内部ConstraintLayout指定其最大宽度,然后让ImageView该内部 ConstrainLayout 的子级指定其百分比来解决此问题。

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

    <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#eee"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintWidth_max="414dp">

        <ImageView
            android:id="@+id/main_imageview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            android:background="#caf"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintDimensionRatio="H,6:5"
            app:layout_constraintWidth_percent="0.666"/>

        <ImageView
            android:id="@+id/second_imageview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            android:background="#fac"
            app:layout_constraintTop_toTopOf="@+id/main_imageview"
            app:layout_constraintLeft_toRightOf="@+id/main_imageview"
            app:layout_constraintBottom_toTopOf="@+id/third_imageview"
            app:layout_constraintDimensionRatio="W,6:5"/>

        <ImageView
            android:id="@+id/third_imageview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            android:background="#afc"
            app:layout_constraintTop_toBottomOf="@+id/second_imageview"
            app:layout_constraintLeft_toRightOf="@+id/main_imageview"
            app:layout_constraintBottom_toBottomOf="@+id/main_imageview"
            app:layout_constraintDimensionRatio="W,6:5"/>

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述