ConstraintLayout
ConstraintLayout 是一个使用“相对定位”灵活地确定组件的位置和大小的一个布局,解决开发中过于复杂的页面层级嵌套过多的问题——层级过深会增加绘制界面需要的时间。
1、基本约束
1.1 相对定位
相对定位是在 ConstraintLayout 中创建布局的基本构建块之一。这些约束允许您相对于另一个控件定位给定的控件。您可以在水平和垂直轴上约束一个控件:
水平轴:左、右、起点和终点
垂直轴:顶部、底部和文本基线
一般概念是将控件的给定一侧约束到任何其他控件的另一侧。
例如,为了将按钮 B 定位到按钮 A 的右侧(图 1):

<Button android:id="@+id/buttonA" ... />
<Button android:id="@+id/buttonB" ...
app:layout_constraintLeft_toRightOf="@+id/buttonA" />
这告诉系统我们希望按钮 B 的左侧被约束到按钮 A 的右侧。这样的位置约束意味着系统将尝试让两侧共享相同的位置。

图 2 - 相对定位约束
这是可用约束的列表(图 2):
<!-- 我的什么位置在谁的什么位置 -->
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
<!--美国佬左右-->
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
<!--文案基线对齐-->
layout_constraintBaseline_toBaselineOf
它们都引用id另一个控件,或parent(将引用父容器,即 ConstraintLayout)
1.2 边距

图 3 - 相对定位边距
如果设置了侧边距,它们将应用于相应的约束(如果存在)(图 3),将边距强制为目标端和源端之间的空间。通常的布局边距属性可用于此效果:
android:layout_marginStart
android:layout_marginEnd

开发指南&spm=1001.2101.3001.5002&articleId=125335184&d=1&t=3&u=ae6d85f5ba644f10a10e1f9368e65173)
8961

被折叠的 条评论
为什么被折叠?



