android新特性,constraintlayout实操
参考资料: http://blog.csdn.net/guolin_blog/article/details/53122387
https://developer.android.google.cn/reference/android/support/constraint/ConstraintLayout.html
一、constraintlayout简介
ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在去年的I/O大会上重点宣传的一个功能。在传统的Android开发当中,界面基本都是靠编写XML代码完成的。虽然Android Studio也支持可视化的方式来编写界面,但是操作起来并不方便。ConstraintLayout就是为了解决这一现状而出现的,可以方便的进行可视化的编写界面。
ConstraintLayout还有一个优点,它可以有效地解决布局嵌套过多的问题。ConstraintLayout则是使用约束的方式,来指定各个控件的位置和关系的。它有点类似于RelativeLayout,但远比RelativeLayout要更强大。
二、约束类型
2.1以下是目前能使用的约束类型:
- Relative positioning
- Margins
- Centering positioning:
- Circular positioning(具体参阅开头资料)
- Visibility behavior (具体参阅开头资料)
- Dimension constraints (具体参阅开头资料)
- Chains (具体参阅开头资料)
##Relative positioning
以下是相对约束的类型:
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_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
##Margins:
The usual layout margin attributes can be used to this effect:
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
Note that a margin can only be positive or equals to zero, and takes a Dimension.
##center position
Bias
layout_constraintHorizontal_biaslayout_constraintVertical_bias
<android.support.constraint.ConstraintLayout ...>
<Button android:id="@+id/button" ...
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent/>
</>
2.2常见操作
- 建立项目:添加constraintlayout依赖
- other layout convert to constraintlayout
- 删除约束
- 改变组件的宽度和高度的模式
##建立项目:添加constraintlayout依赖
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
##other layout convert to constraintlayout
##删除约束方法
下图中,1为单个约束删除,2为删除单个组件的所有约束,3为删除所有组件的所有约束
##改变组件的宽度和高度的模式
位于Inspector最中间的那个正方形区域,它是用来控制控件大小的。一共有三种模式可选,每种模式都使用了一种不同的符号表示,点击符号即可进行切换。
![]()
![]()
表示wrap content,这个我们很熟悉了,不需要进行什么解释。
![]()
表示固定值,也就是给控件指定了一个固定的长度或者宽度值。
![]()
表示any size,它有点类似于match parent,但和match parent并不一样,是属于ConstraintLayout中特有的一种大小控制方式,下面我们来重点讲解一下。
三、guideline
虚拟组件,辅助页面布局。
- Horizontal guidelines
- Vertical guidelines
##guideline
四、自动添加约束
- Autoconnect:打开该开关按钮,为添加的单个组件自动添加约束
- infer Constraints:点击该按钮,为页面所有组件自动添加约束
autoconnect
infer Constraints


The usual layout margin attributes can be used to 



浙公网安备 33010602011771号