1、控件的宽、高都不可设置为 match_parent,否则所有的约束失效
2、欲使设置的margin生效,须指定控件的约束对象。例如,欲使marginTop生效,须设定layout_constraintTop_toTopOf
3、被Group持有的所有控件,其visiblity只能由Group决定
4、以当前布局中不能准确知道高度的View为标准,例如:在一个布局中,左边TextView的内容是固定的文字,右边TextView的内容须根据具体业务进行动态设置,则其高度是不确定的,这种情况下高度约束均以右边为中心
5、自定义组合控件使用ConstraintLayout作为布局的根时,使用LinearLayout作为自定义控件的父布局时会造成自定义封装的布局展示不正确(宽高变形,与测量方式有关)。推荐使用FrameLayout作为根布局,同时固定高度
6、欲使layout_constraintVertical_bias属性生效,须设置上下约束。同样欲使layout_constraintHorizontal_bias属性生效,须设置左右约束
7、若ConstraintLayout不是根布局,则其中的子控件的约束不能指定为 parent,但可指定为父控件ConstraintLayout的id
8、layout_marginLeft和layout_marginRight不起作用:控件左右都约束了parent,但有时左右margin不起作用,可能是因为layout_width设置成了match_parent或者wrap_content,须改为match_constraint(即0dp)才会起作用
9、Guideline:
(1)layout_constraintGuide_percent属性值可以为负值,有时某些特殊的需求会用到
(2)对Guideline设置相对位置属性是不生效的,因此当我们想要一个相对于某个view的Guideline时,约束布局是不能满足的。此时可以用一个不可见的View作为Guideline的替代品来实现一些特殊布局需求,如布局重叠,这种方式可以弥补margin不能设置为负值的不足
10、Chain:
(1)在chainStyle=spread时才可以使用加权layout_constraintHorizontal_weight和layout_constraintVertical_weight 。
(2)注意Chain中的控件互相引用id的格式,是“@+id/button1”而非”@id/button1”。
(3)可以用Chain实现控件居中
11、注意View.inflate(getActivity(), R.layout.item, null)和LayoutInflater.from(getActivity()).inflate(R.layout.item, parent, false)区别,有时二者对ConstraintLayout布局会有不同的影响
12、在ConstraintLayout中,控件除了可以设置layout_marginXXX外,还可以设置其依赖的控件gone之后的margin,即layout_goneMarginXXX。就是说,可以根据被依赖控件的visibility,设置两种margin
13、使用布局编辑器多出了一些属性
layout_optimizationLevel
layout_editor_absoluteX
layout_editor_absoluteY
layout_constraintBaseline_creator
layout_constraintTop_creator
layout_constraintRight_creator
layout_constraintLeft_creator
layout_constraintBottom_creator
这些属性是 UI 编辑器用来辅助拖拽布局的,在实际使用过程中可以不用关心它们