手机移动程序开发安卓资源收集Android程序猿

ConstraintLayout详解与使用方法

2018-03-20  本文已影响50人  mengming

ConstrainLayout 是 AndroidStudio2.2 的新特性,能够很方便的用于可视化开发,有点类似于 ios 的 storyboard,解决了 xml 过于复杂的问题。


约束

相对定位(Relative positioning)

也就是我们之前常用的relativelayout,属性也与其一致,使用方法如:layout_constraintLeft_toLeftOf,位置用一张图片解释。


边距(Margins)

同之前的margin使用方法。


居中定位(Centering positioning)
通过定义上下左右的相邻控件或父容器来设置居中。可以形象的理解为两个大小相同,方向相反的力作用在控件上。

如下,通过定义top, bottom, start, end就可以使textview在parent居中显示。

<TextView
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="TextView"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent" />

倾向(bias)
搭配bias,就像两个力大小不相等,使其往一个方向靠拢。 bias = 0.3
圆定位(Circular positioning)(1.1新特性)
可以理解为以一个控件为圆心,设定半径和角度,决定另一个控件的位置。如图
可见性行为(Visibility behavior)

这是针对View.GONE的特定处理。Gone隐藏的控件,会被当成一个点,同时忽略margin。如图。


尺寸约束(Dimensions constraints)

比例(Ratio)
     <Button android:layout_width="0dp"
     android:layout_height="0dp"
     app:layout_constraintDimensionRatio="H,16:9"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintTop_toTopOf="parent"/>

链条(Chains)

虚拟辅助元素(Virtual Helper objects)

可以使用辅助对象来创建相对约束,可以通过水平或垂直的Guideline来定位控件。


可视化操作

约束控键类型
添加约束
删除约束
Inspector的使用

这个界面就是控件的属性。我们除了可以修改一些基本属性,还有一些新的功能。


总结

参考官方文档
用constraintLayout可以完全替换掉之前的linearLayout和relativeLayout,使用也很方便,希望能帮大家快速上手。

上一篇下一篇

猜你喜欢

热点阅读