本章节将分为两个模块给大家推送,觉得不错的可以关注下哦
介绍
Android ConstraintLayout是谷歌推出替代PrecentLayout的组件。
支持相对布局、线性布局、帧布局,看来更像是FrameLayout 、LinearLayout、`RelativeLayout·三者的结合体,并且比这三者更强大的是实现了百分比布局。
大家都知道安卓碎片严重,使用百分比适配,那么将彻底解决适配问题
总结:我最近也是刚学,学完之后,发现这个布局已经将上述的所有布局的特点全部融合在一起了,使用起来简单方便的不要不要的,就是学习的属性有点多啊。
不过,多也是正常的,毕竟融合了五大布局的所有特点,学完这个布局,各种界面UI都难不倒我们了
添加依赖
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
我使用的是Android Studio3.0.1版本,已经自动导入了,默认使用的就是这个布局
属性及对应的方法
我们先了解一下一些基本的概念
![4dfe80687828d17fbe1e0e719fb578bc.png](https://img-blog.csdnimg.cn/img_convert/4dfe80687828d17fbe1e0e719fb578bc.png)
这里提一下,start和left其实都是指控件的左边,end和right都事指控件的右边
基本属性
注意,这里的属性都得使用命名空间来才能使用
宽高属性与之前的layout相同,wrap_content
和match_parent
,但除此之外,还多出了一种,名为match contraint
实际上,这个多出来的相当于0dp
,如果之前使用过LinearLayout
的权重的话,应该对0dp
有印象.
这里,约束布局应该是继承了Linearlayout
的特性,之后我们设置权重与Linearlayout
的操作也是类似,具体的请往后面看,这可是实现了百分比布局的强大布局。
属性值为控件id
layout_constraintLeft_toLeftOf
当前控件的左边依赖于属性控件的左边layout_constraintLeft_toRightOf
当前控件的左边依赖于属性控件的右边layout_constraintRight_toLeftOf
ayout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
当前的控件基线与属性控件的基线对齐layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
示例:
![d12409d3ecf5b66212c88bb4406799e2.png](https://img-blog.csdnimg.cn/img_convert/d12409d3ecf5b66212c88bb4406799e2.png)
![25262a8085173f914f619ad4ddf531b0.png](https://img-blog.csdnimg.cn/img_convert/25262a8085173f914f619ad4ddf531b0.png)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.c