您的位置:首页 > 大数据 > 人工智能

ConstraintLayout布局学习笔记

2017-02-12 11:31 429 查看
参考资料:

Constraint 代码实验室

Android新特性介绍,ConstraintLayout完全解析

主要知识点:

1、手动创建约束

2、自动创建约束

3、推理创建约束

1、引入 compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'
在layout中Palette中点击ConstraintLayout会自动引入

2、同一个方向上的两个约束必须成对出现,即constraintBottom和constraintTop或者constraintLeft和constraintRight同时出现时,layout_constraintVertical_bias才生效,这时候才能看到有波浪线。

3、layout_constraintVertical_bias
总的高度减去该控件的高度再减去该控件的margintop和marginbottom剩余的高度我们用100%表示,
那么layout_constraintVertical_bias的值表示的就是该控件上边的外边距占剩余高度的比例。

4、constraintDimensionRatio
让控件的宽高以固定的比例展示(如:app:layout_constraintDimensionRatio="1:3"表示宽高比为1:3)
这个属性生效需要以下条件:

left ,right ,top ,bottom四条边都需要约束,bottom的约束可以用baseline代替。

宽高至少有一个是0dp或者-1dp,如果都是0dp的则会以父布局能容下的最大范围显示。

5、在ConstraintLayout的子布局中如果有ViewGroup类型的控件(如:相对布局,线性布局)这类控件的子空间不能使用约束的属性。

6、Autoconnect 仅为当前正在移动的控件创建约束是很重要。一般会有虚线出现时表示自动约束成功。

7、推理创建的约束依赖于添加到布局的元素的类型和他们的尺寸.。

8、 *_creator属性,默认是0表示对应的约束是人为创建的,1表示是推理创建的约束。

9、ConstrainLayout支持三种尺寸:Exact,WrapContent,AnySize;不支持match_parent,简单的说就是Any Size就已经实现了match_parent的功能,将layout_width或layout_heigth设为0dp即可。

10、Guideline只支持他自己的四中属性(orientation layout_constraintGuide_begin layout_constraintGuide_end layout_constraintGuide_percent),点击箭头可以在后三种模式间切换。

11、alpha7中去掉了layout_constraintCenterX_toCenterX和layout_constraintCenterY_toCenterY 属性,如果想让View2和View1居中对齐,可以这样:

app:layout_constraintStart_toStartOf="xxxid"
app:layout_constraintEnd_toEndOf="xxxid"
app:layout_constraintHorizontal_bias=".5"

12、Chain Style

a、一组控件全选中,应用工具栏的Center Horizatolly或者Center Vertical哪个方向上的距离大,哪个方向的Chain Style才生效,所以想在哪个方向上应用Chain Style,应该先把一组view在哪个方向上摆差不多齐

b、经过第一步后,再任意选中该组中的view,点击链条状的图标,就可以在第一个view(Chain head)上自动添加属性layout_constraintVertical_chainStyle,连续点击会在几种模式间切换。

c、Chain 的属性由该群组的第一个View也就是Chain head 上的属性所控制,所以第二部中会自动把属性添加在第一个view上。

d、总共四个属性
layout_constraintHorizontal_chainStyle
layout_constraintHorizontal_weight
layout_constraintVertical_chainStyle
layout_constraintVertical_weight

e、layout_constraintHorizontal_weight和layout_constraintVertical_weight与LinearLayout中类似,如需要把尺寸设置为0dp,只有在spread或者spread_inside模式下有效
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: