layout_constraint[SourceAnchor]_[TargetAnchor]="[TargetId]"
其中 SourceAnchor / TargetAnchor 可以是:
layout_constraintHorizontal_bias=左方占比 layout_constraintVertical_bias=上方占比 特征:
measure大小始终是0;
2.它的 visibility 始终是 View.GONE
layout_constraintGuide_begin layout_constraintGuide_end layout_constraintGuide_Percent P.S: Guideline是纵向还是横向的,由 android:orientation="<vertical|horizontal>" 决定。
layout_editor_absoluteX 与 layout_editor_absoluteY 是只有在编辑器中使用,设备上运行时这些是没有效果的。参数空间是在: http://schemas.android.com/tools 。
match_parent 可替代: 将 layout_width / layout_height 设为 0dp : 填充满附属的锚点布局。
原因: 但是更加灵活,可以类似以前的 layout_weight 使用。
layout_constraintDimensionRatio 提供长宽比如: 4:3 。在给了这个参数的情况下,需要提供任意一边的值(指定值或 wrap_content )
添加子View到 ContraintLayout 时,所有XML中 layout_ 前缀的属性都会添加到 LayoutParams 实例中。
ConstraintLayout 子View中的 LayoutParams 存储着 ConstraintWidget , ConstraintWidget 用于逻辑运算与分析,并且每个 ConstraintWidget 与 ConstraintLayout 中的 ConstraintWidgetContainer 相联系。
ConstraintWidget 持有所有关于View位置与大小的信息,在measure与layout时作为数据依据。
在layout的期间,会根据 LayoutParams 中constraint的信息,为 ConstraintWidget 中每个连接的锚点定义 ConstraintAnchor 。
这里的”Any Size”View,指的是XML中给的长或宽为 0dp 的View。
“Any Size”View,需要两次运算,首次会直接不做检查在 ConstraintLayout#internalMeasureChildren 中直接使用 WRAP_CONTENT 用于计算大小( ViewGroup#getChildMeasureSpec ),第二次根据其他的计算结果在 ConstraintLayout#onMeasure 中计算出真正的大小。
P.S: Add Constraints to Equation Solver 、 Minimize Linear System 、 Update Child Bounds from solution 都是在 ConstraintLayout#onMeasure 中在执行的 ConstraintLayout#internalMeasureChildren 。该算法计通过 LinearSystem 算出了View的bound。
由于上面在measure时已经估算出各view的大小与位置,因此在此之后 ConstraintWidget 中已经有对应View适当的bound了。因此在 onLayout 中只需要遍历所有子View,设置他们的bound值就行,十分轻量。
for(int i = 0; i < getChildCount(); ++i) {
View child = this.getChildAt(i);
ConstraintLayout.LayoutParams params =
(ConstraintLayout.LayoutParams)child.getLayoutParams();
ConstraintWidget widget = params.widget;
int l = widget.getDrawX();
int t = widget.getDrawY();
int r = l + widget.getWidth();
int b = t + widget.getHeight();
child.layout(l, t, r, b);
}
由于 ConstraintLayout 有效的优化了layout与draw,并且从质上有效的减少了层级,因此相同的布局呈现上,通常情况下 ConstrantLayout 的性能都比其他的Layout性能要好。
文中图片全部来源: http://wiresareobsolete.com/2016/07/constraintlayout-part-2/
© 2012 - 2016, Jacksgong(blog.dreamtobe.cn). Licensed under the Creative Commons Attribution-NonCommercial 3.0 license (This license lets others remix, tweak, and build upon a work non-commercially, and although their new works must also acknowledge the original author and be non-commercial, they don’t have to license their derivative works on the same terms). http://creativecommons.org/licenses/by-nc/3.0/