ConstraintLayout纵横比

ConstraintLayout aspect ratio

考虑以下布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0000"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#0000FF"
            android:padding="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintDimensionRatio="H,3:1"
            tools:layout_editor_absoluteX="16dp" />

    </android.support.constraint.ConstraintLayout>

</RelativeLayout>

我不确定 app:layout_constraintDimensionRatio 是如何工作的。我的理解是比率永远是 width:height。所以 3:1 总是会让 ImageView 看起来比高度宽 3 倍。前缀 H 或 W 告诉 ConstraintLayout 哪个维度应该遵守比率。如果它是 H 那么它意味着宽度将首先从其他约束计算出来,然后高度将根据纵横比进行调整。然而,这是布局的结果:

高度是宽度的3倍,出乎意料。任何人都可以向我解释如何根据 app:layout_constraintDimensionRatio 设置计算尺寸吗?

看看这些 ImageView 属性:

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"

这些属性覆盖了 layout_constraintDimensionRatio,因此 ImageView 被限制在主父级的底部、顶部和左侧,导致 View 占据了左侧、顶部和主屏幕的底部。

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"

这将是一种解决方案。如果您希望视图显示在顶部,则可以省略 layout_constraintBottom_toBottomOf ,反之亦然。最好 完全删除上述所有约束 除了 layout_constraintDimensionRatio,这将是最 推荐的 解决方案。

您对 app:layout_constraintDimensionRatio 工作方式的理解是正确的。如果你设置 app:layout_constraintDimensionRatio="H,3:1" 那么这意味着宽度将首先从其他约束计算出来,然后高度将根据纵横比进行调整。您的实现的唯一问题是您将 app:layout_constraintBottom_toBottomOf="parent" 添加到 ImageView,因此它导致 app:layout_constraintDimensionRatio 被忽略。

这是按 3:1 纵横比调整 ImageView 大小的布局:

<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:background="#FF0000">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:background="#0000FF"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintDimensionRatio="H,3:1" />

</android.support.constraint.ConstraintLayout>

这是结果视图:

基本上,我们有

layout_constraintDimensionRatio(width:height)

示例

<!-- button which have width = it's content and height = 1/2 width -->
<Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent" <!-- I still think that we don't need this attribute but I when I don't add this, constraint not working -->
        android:text="Button TEST RATIO 1"
        app:layout_constraintDimensionRatio="2:1" />

输出

<!-- button which have width = it's content and height = 1/2 width -->
<Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        android:text="Button TEST RATIO 2"
        app:layout_constraintDimensionRatio="2" /> <!-- 2 here <=> 2:1 <=> 2/1 (1:1 <=> 1, 1/2 <=> 0.5, ....) ->

输出

<!-- button which have width = match_parent and height = 1/2 width -->
<Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="Button TEST RATIO 3"
        app:layout_constraintDimensionRatio="2" />

输出

<!-- button which have width = match constraint and height = 1/2 width -->
<Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button TEST RATIO 4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintDimensionRatio="2" />

输出

演示:https://github.com/PhanVanLinh/AndroidConstraintLayoutRatio