【问题标题】:ConstraintLayout aspect ratio约束布局纵横比
【发布时间】:2017-05-07 01:00:15
【问题描述】:

考虑以下布局文件:

<?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 则意味着首先从其他约束计算宽度,然后根据纵横比调整高度。然而这是布局的结果:

高度是宽度的 3 倍,这是出乎意料的。谁能向我解释如何根据 app:layout_constraintDimensionRatio 设置计算尺寸?

【问题讨论】:

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


    【解决方案1】:

    您对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>
    

    这是结果视图:

    【讨论】:

    • 使用 androidx 是androidx.constraintlayout.widget.ConstraintLayout
    【解决方案2】:

    基本上,我们有

    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

    【讨论】:

      【解决方案3】:

      看看这些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 之外,最好完全删除所有上述限制,这将是最推荐的解决方案。

      【讨论】:

        猜你喜欢
        • 2020-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-05
        • 1970-01-01
        • 2017-01-20
        • 1970-01-01
        • 2015-03-22
        相关资源
        最近更新 更多