开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第25天,点击查看活动详情
ConstraintLayout可以在API 9以上的Android操作系统使用它,它主要是为了解决布局嵌套过多的问题而出现,以灵活的方式定位和调整小部件。从Android Studio 2.3起,官方的模板默认使用ConstraintLayout。ConstraintLayout与RelativeLayout类似,采用相对定位的布局模式。ConstraintLayout的灵活性要高于RelativeLayout,性能更出色!还有一点是ConstraintLayout可以按照比例约束控件位置和尺寸,能更好地适配屏幕大小不同地机型。。
ConstraintLayout相对定位
| 属性名称 | 描述 |
|---|---|
| layout_constraintTop_toTopOf | 期望视图的上边对齐另一个视图的上边 |
| layout_constraintTop_toBottomOf | 期望视图的上边对齐另一个视图的底边 |
| layout_constraintTop_toLeftOf | 期望视图的上边对齐另一个视图的左边 |
| layout_constraintTop_toRightOf | 期望视图的上边对齐另一个视图的右边 |
| layout_constraintTop_toTopOf | 期望视图的下边对齐另一个视图的上边 |
| layout_constraintTop_toBottomOf | 期望视图的底边对齐另一个视图的底边 |
| layout_constraintTop_toLeftOf | 期望视图的底边对齐另一个视图的左边 |
| layout_constraintTop_toRightOf | 期望视图的底边对齐另一个视图的右边 |
ConstraintLayout基线定位
| 属性名称 | 描述 |
|---|---|
| layout_constraintBaseline_toBaselineOf | Baseline指的是文本基线,文本对齐 |
ConstraintLayout角度定位常用属性
| 属性名称 | 描述 |
|---|---|
| layout_constraintCircle | 参照控件的id |
| layout_constraintCircleAngle | 当前View的中心与目标View的中心的连线与Y轴方向的夹角(取值0到360) |
| layout_constraintCircleRadius | 两个控件中心连线的距离 |
ConstraintLayout边距定位常用属性
| 属性名称 | 描述 |
|---|---|
| layout_marginBottom | 离某元素底边缘的距离 |
| layout_marginLeft | 离某元素左边缘的距离 |
| layout_marginRight | 离某元素右边缘的距离 |
| layout_marginTop | 离某元素上边缘的距离 |
| layout_marginStart | 如果在LTR布局(从左到右布局)模式下,该属性等同于layout_marginLeft ,如果在RTL布局模式下,该属性等同于layout_marginRight |
| layout_marginEnd | 如果在LTR布局(从左到右布局)模式下,该属性等同于layout_marginRight ,如果在RTL布局模式下,该属性等同于layout_marginLeft |