ConstraintLayout

简介

ConstraintLayout(即约束布局)发布于2016年Google I/O大会。布局嵌套过多有性能问题,RelativeLayout或GridLayout可减布局嵌套,现可用ConstraintLayout减布局层级结构。相比RelativeLayout性能更好且更易用,结合Android Studio布局编辑器可实现拖拽控件编写布局等等。

资料

依赖

新建工程Android Studio默添ConstraintLayout依赖。改旧工程于build.gradle添:

implementation 'com.android.support.constraint:constraint-layout:1.1.2'

属性

位置
layout_constraintLeft_toLeftOf
约束左于某控件左
layout_constraintLeft_toRightOf
约束左于某控件右
layout_constraintRight_toLeftOf
约束右于某控件左
layout_constraintRight_toRightOf
约束右于某控件右
layout_constraintTop_toBottomOf
约束上于某控件下
layout_constraintBottom_toTopOf
约束下于某控件上
layout_constraintBottom_toBottomOf
约束下于某控件下
layout_constraintStart_toEndOf
约束开始于某控件结束
layout_constraintStart_toStartOf
约束开始于某控件开始
layout_constraintEnd_toStartOf
约束结束于某控件开始
layout_constraintEnd_toEndOf
约束结束于某控件结束
layout_constraintBaseline_toBaselineOf
MATCH_PARENT

官网

Important: MATCH_PARENT is not supported for widgets contained in a ConstraintLayout, though similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".

ConstraintLayout不支持MATCH_PARENT,可通MATCH_CONSTRAINT配合约束实现类似效果。

使用

居中

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"

缺点

调布局特别删控件时依赖关系较多则麻烦。Google建用ConstraintLayout替RelativeLayout。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snpmyn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值