ConstraintLayout用法全解析

ConstraintLayout用法全解析

2023年7月13日发(作者:)

ConstraintLayout⽤法全解析*本篇⽂章已授权微信公众号 guolin_blog (郭霖)独家发布本⽂是基于constraint-layout:1.1.2⼀、前⾔在以前,android是使⽤布局如LinearLayout 、RelativeLayout等来构建页⾯,但这些布局使⽤起来很⿇烦,并且经常需要⼀层⼀层嵌套,写⼀个简单的页⾯就需要费很⼤的劲。所以在16年I/O⼤会上,google发布了全新的布局-ConstraintLayout,其他布局和ConstraintLayout⽐起来,根本就没有存在的必要了...ConstraintLayout具有以下优势:1. 较⾼的性能优势。布局嵌套层次越⾼,性能开销越⼤。⽽使⽤ConstraintLayout,经常就⼀层嵌套就搞定了,所以其性能要好很多。详细的性能分析可参见:解析ConstraintLayout的性能优势2. 完美的屏幕适配ConstraintLayout的⼤⼩、距离都可以使⽤⽐例来设置,所以其适配性更好。3. 书写简单4. 可视化编辑。ConstraintLayout也有⼗分⽅便完善的可视化编辑器,不⽤写xml也基本上能实现⼤部分功能。但个⼈还是⽐较喜欢写xml,所以本篇⽂章主要介绍如何使⽤代码控制。如果想看如何使⽤可视化编辑器,可以参考郭霖⼤神的这篇⽂章引⼊:api 'aint:constraint-layout:1.1.2'⼆、ConstraintLayout1. 定位位置确定位置的属性提供了下⾯13个属性,其实本质上都是⼀样的,看名字应该基本上都知道怎么⽤了(就是哪⼀条边和哪⼀条边对齐)layout_constraintLeft_toLeftOflayout_constraintLeft_toRightOflayout_constraintRight_toLeftOflayout_constraintRight_toRightOflayout_constraintTop_toTopOflayout_constraintTop_toBottomOflayout_constraintBottom_toTopOflayout_constraintBottom_toBottomOflayout_constraintBaseline_toBaselineOflayout_constraintStart_toEndOflayout_constraintStart_toStartOflayout_constraintEnd_toStartOflayout_constraintEnd_toEndOf来看个例⼦:实现上述UI的相关代码如下:

...>

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信