ConstraintLayout布局里的一个属性app: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>

效果图:那么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>

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

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值