android - 约束布局纵横比

标签 android android-layout scale aspect-ratio android-constraintlayout

考虑以下布局文件:

<?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 是如何工作的。我的理解是比例永远是宽度:高度。所以 3:1 总是会让 ImageView 看起来比高度宽 3 倍。前缀 H 或 W 告诉 ConstraintLayout 哪个维度应该遵守比率。如果是 H 则意味着首先从其他约束计算宽度,然后根据纵横比调整高度。然而这是布局的结果:

enter image description here

高度是宽度的 3 倍,这是出乎意料的。谁能向我解释如何根据 app: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>

这是结果 View :

ImageView in 3:1 aspect ratio

关于android - 约束布局纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41265570/

相关文章:

android - Cardview 阴影没有出现在 Lollipop 设备中?

android - ScrollView + 缩放图像 = 无法正确滚动,为什么不呢?

android - 像主屏幕滑动动画一样的 ImageSwitcher 动画

View 上的Android动态气泡

android - 获取Android App的 'root"权限

java - 如何在后按时执行某些操作,而不是仅针对一个 fragment 从 backStack 弹出,并且仍然能够对其他 fragment 正常工作?

python - 更改 Altair 图中的图例编号范围

javascript - 使元素在 css 缩放后表现相同

java - Facebook 登录导致应用程序崩溃并出现 java.lang.ClassCastException : LoginActivity must implement OnFragmentInteractionListener

java - MPAndroidChart:为 Y 轴提供一些偏移量