如何为两个View设置约束?附ConstraintLayout代码示例
为ConstraintLayout中的两个View设置约束的方案
看你当前的代码,view1已经设置了垂直居中且靠右的约束,但view2的约束和它几乎完全一致(还多了个笔误aapp:),这样两个View会直接重叠在一起。下面给你几种常见布局场景对应的约束设置方案,你可以根据需求选择:
场景1:两个View在父布局右侧水平并排(垂直居中)
这种方案会让两个View在屏幕右侧横向排列,同时保持垂直居中,适合做操作按钮组:
<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"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="示例文本" app:layout_constraintEnd_toStartOf="@+id/view1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <View android:id="@+id/view1" android:layout_width="60dp" android:layout_height="60dp" android:background="#FF0000" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintMarginEnd="16dp"/> <!-- 增加右边距避免贴边 --> <View android:id="@+id/view2" android:layout_width="60dp" android:layout_height="60dp" android:background="#00FF00" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/view1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintMarginEnd="8dp"/> <!-- 两个View之间的间距 --> </androidx.constraintlayout.widget.ConstraintLayout>
场景2:两个View在父布局右侧垂直排列(水平靠右)
如果需要让两个View在屏幕右侧上下堆叠,可以这样设置:
<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"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="示例文本" app:layout_constraintEnd_toStartOf="@+id/view1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <View android:id="@+id/view1" android:layout_width="60dp" android:layout_height="60dp" android:background="#FF0000" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintMarginTop="16dp" app:layout_constraintMarginEnd="16dp"/> <View android:id="@+id/view2" android:layout_width="60dp" android:layout_height="60dp" android:background="#00FF00" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/view1" app:layout_constraintMarginTop="8dp" app:layout_constraintMarginEnd="16dp"/> </androidx.constraintlayout.widget.ConstraintLayout>
场景3:两个View平分父布局剩余宽度(垂直居中)
要是想让两个View横向平分TextView右侧的剩余空间,可以用约束链+权重实现:
<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"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="示例文本" app:layout_constraintEnd_toStartOf="@+id/view1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintMarginEnd="16dp"/> <View android:id="@+id/view1" android:layout_width="0dp" <!-- 0dp配合约束实现自适应宽度 --> android:layout_height="60dp" android:background="#FF0000" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/view2" app:layout_constraintStart_toEndOf="@+id/text_view" app:layout_constraintTop_toTopOf="parent" app:layout_constraintHorizontal_weight="1"/> <!-- 权重1,平分空间 --> <View android:id="@+id/view2" android:layout_width="0dp" android:layout_height="60dp" android:background="#00FF00" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/view1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintHorizontal_weight="1"/> </androidx.constraintlayout.widget.ConstraintLayout>
最后提醒下:你代码里view2的属性写成了aapp:layout_constraintBottom_toBottomOf,多了一个a,记得修正为app:哦。
内容的提问来源于stack exchange,提问作者litaoshen
相关产品推荐
相关产品推荐

