ConstraintLayout简介
ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在去年的I/O大会上重点宣传的一个功能。
ConstraintLayout 很可能比传统布局的性能更出色。不仅如此,ConstraintLayout 还具备其他一些功能,能够帮助您构建复杂的高性能布局。ConstraintLayout 拥有完全扁平的层次结构,允许您构建复杂的布局,而不必嵌套 View 和 ViewGroup 元素。关于ConstraintLayout的性能优势请参阅:解析ConstraintLayout的性能优势
使用ConstraintLayout
添加ConstraintLayout的依赖
使用ConstraintLayout,我们需要在app/build.gradle文件中添加ConstraintLayout的依赖,如下所示:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Android Studio 3.0.1创建的android项目布局默认使用ConstraintLayout
如果你使用的版本默认不使用ConstraintLayout,仍然可以Convert the layout:
参考:https://developer.android.google.cn/training/constraint-layout/index.html#add-a-constraint
如图选中Design后显示出操作界面:
主操作区域内有两个类似于手机屏幕的界面,左边的是预览界面,右边的是蓝图界面。这两部分都可以用于进行布局编辑工作,区别是左边部分主要用于预览最终的界面效果,右边部分主要用于观察界面内各个控件的约束情况。
点击显示Hello World的TextView控件,最右侧出现Properties区域
ConstraintLayout 指导
Relative positioning
Margins
Centering positioning
Circular positioning
Visibility behavior
Dimension constraints
Chains
Virtual Helpers objects
例如:
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
详细请参阅:
https://developer.android.google.cn/reference/android/support/constraint/ConstraintLayout.html
另外可以参阅:https://medium.com/google-developers/building-interfaces-with-constraintlayout-3958fa38a9f7
里面对于Chains、Ratios、Guidelines、Dealing with View.GONE有详细的解读
ConstraintLayout操作
请阅读:http://blog.csdn.net/guolin_blog/article/details/53122387