一、概述
ConstraintLayout 是一个 ViewGroup,它可以解决复杂布局视图嵌套问题,创建布局一般为平面视图层次结构。跟 ReletiveLayout 很像,所有的 view 通过和相邻的 view 或者 parent layout 之间建立关系来布局,但是比 ReletiveLayout 更加灵活、简单,而且减少绘制。
二、添加约束
// 位置约束
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="parent"
添加了约束之后,对应方向上的 margin 值才有效。下边是几种常用的约束,可以实现不同的布局效果。
- parent 约束
app:layout_constraintLeft_toLeftOf="parent"
- id 约束

本文详细介绍了Android的ConstraintLayout,包括其解决复杂布局的问题、添加约束的方法、View大小调整、链(Chains)的使用以及关键帧动画的实现。约束布局提供了一种更灵活的方式来布局视图,减少了视图层级并简化了布局设计。
最低0.47元/天 解锁文章


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



