赞
踩
这是一个约束布局,可以尽可能的减少布局的嵌套。有一个属性特别好用,可以用来动态限制宽或者高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)
<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)
<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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。