当前位置:   article > 正文

ConstraintLayout布局里的一个属性app:layout_constraintDimensionRatio

layout_constraintdimensionratio

ConstraintLayout

这是一个约束布局,可以尽可能的减少布局的嵌套。有一个属性特别好用,可以用来动态限制宽或者高app:layout_constraintDimensionRatio

关于app:layout_constraintDimensionRatio参数

app:layout_constraintDimensionRatio=“h,1:1”
表示高度height是动态变化的,然后比例是 width / height

app:layout_constraintDimensionRatio=“w,1:1”
表示宽度width是动态变化的,然后比例是 width / height

有两种情况,主要介绍第2种
1.自身的width和height都是0dp ( 这种情况的比例 width / height得慢慢去调,没那么准确,可以自己去尝试,不在这里做介绍。)

2.自身的width和height其中一个是0dp,match_parent, wrap_content, 一个固定的值(100dp)

  • width是0dp,height是一个固定值
    height是200dp,那么width = 200dp * (1/2)

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff1111"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <View
            android:layout_width="0dp"
            android:layout_height="200dp"
            android:background="#63db37"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="w,1:2"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

效果图:那么width = 200dp * (1/2)
在这里插入图片描述

  • height是0dp, width是一个固定值
    width是200dp,那么height = 200dp * (2/1)
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff1111"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <View
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:background="#63db37"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="h,1:2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

效果图:那么height = 200dp * (2/1)

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/225642
推荐阅读
相关标签
  

闽ICP备14008679号