ConstraintLayout

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安德路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值