LinearLayout在另一个ConstraintLayout中水平垂直居中
在Android的ConstraintLayout中,使用布局约束来确保一个LinearLayout在水平垂直方向上居中。
<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">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
使用app:layout_constraint*_to*Of
属性来约束这个LinearLayout
,使其在所有四个方向上都与父容器接触。
app:layout_constraintLeft_toLeftOf="parent"
和app:layout_constraintRight_toRightOf="parent"
确保LinearLayout
在水平方向上与父容器的左右边界对齐。
app:layout_constraintTop_toTopOf="parent"
和app:layout_constraintBottom_toBottomOf="parent"
确保LinearLayout
在垂直方向上与父容器的前后边界对齐。
app:layout_constraintVertical_bias="0.5"
设置了垂直方向的偏移量,使得LinearLayout
在垂直方向上居中。