-
百分比布局:
ConstraintLayout
支持控件的尺寸按照父布局尺寸的百分比来设置。通过app:layout_constraintWidth_percent
和app:layout_constraintHeight_percent
属性,可以设置控件的宽度和高度占父布局的百分比比例。需要注意的是,只有当宽度和高度尺寸为0dp
时,百分比属性才会生效。<androidx.constraintlayout.widget.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"> <View android:id="@+id/view" android:layout_width="0dp" android:layout_height="0dp" android:background="#ff0000" app:layout_constraintWidth_percent="0.5" app:layout_constraintHeight_percent="0.3" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
-
宽高比(Ratio):可以通过
layout_constraintDimensionRatio
属性来设置控件的宽高比。例如,设置app:layout_constraintDimensionRatio="1:1"
可以创建一个宽高相等的控件。<androidx.constraintlayout.widget.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"> <View android:id="@+id/view" android:layout_width="0dp" android:layout_height="0dp" android:background="#ff0000" app:layout_constraintDimensionRatio="1:1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
-
圆形定位:
ConstraintLayout
支持将一个控件的中心以一定的角度和距离约束到另一个控件的中心上。这通过layout_constraintCircle
、layout_constraintCircleRadius
和layout_constraintCircleAngle
三个属性来控制。<androidx.constraintlayout.widget
Android中的ConstrainLayout的用法(下)
于 2024-10-15 09:24:59 首次发布