目录
ConstraintLayout是Android新推出的一个布局,其性能更好,连官方的
hello world都用
ConstraintLayout来写了。所以极力推荐使用
ConstraintLayout来编写布局。
本文主要介绍一下如何使用代码来编写ConstraintLayout布局。
关于如何拖拽使用ConstraintLayout,可以去看下郭霖大神写的:Android新特性介绍,ConstraintLayout完全解析
如果对ConstraintLayout的性能比较感兴趣,可以看这篇文章:解析ConstraintLayout的性能优势
好了,开始我们的征程。
1 ConstraintLayout简介
ConstraintLayout,可以翻译为约束布局,在2016年Google I/O 大会上发布。我们知道,当布局嵌套过多时会出现一些性能问题。之前我们可以去通过RelativeLayout或者GridLayout来减少这种布局嵌套的问题。现在,我们可以改用ConstraintLayout来减少布局的层级结构。ConstraintLayout相比RelativeLayout,其性能更好,也更容易使用,结合Android Studio的布局编辑器可以实现拖拽控件来编写布局等等。
2 引入ConstraintLayout
如果我们是新建工程,则Android Studio会默认帮我们加入ConstraintLayout的依赖了。
如果我们是改造旧项目,可以在build.gradle中添加以下依赖:
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
然后就可以使用ConstraintLayout了。
3 相对位置
要在ConstraintLayout中确定view的位置,必须至少添加一个水平和垂直的约束。每一个约束表示到另一个view,父布局,或者不可见的参考线的连接或者对齐。如果水平或者垂直方向上没有约束,那么其位置就是0.
我们先来看个例子:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左对齐"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右对齐"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平居中"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="垂直居中"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="底部对齐"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平居中+垂直居中"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
其显示效果如下图所示:

上面例子中app:layout_constraintLeft_toLeftOf="parent" 表示view的左边对齐父布局的左边。
app:layout_constraintRight_toRightOf="parent"则表示view的右边对齐父布局的右边。
其他同理,就不一一说明。
- 相对位置的属性
下面是ConstraintLayout确定位置的属性。
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
这些属性的值即可以是parent,也可以是某个view的id。
- 居中显示
如果一个view满足以下
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
即view的左边对齐父布局的左边,view的右边对齐父布局的右边,除非这个view的大小刚好充满整个父布局;否则的话,就是水平居中显示了。我们可以理解为有两个力,它们左右互搏,view只能给扯到中间了。
再来两个例子,把上面的属性基本都涉及到了,看下估计就懂了,就不逐一说明了。
- 例子一:水平方向的相对位置
这里主要关注水平方式的属性即可。
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"

本文详细介绍了Android约束布局ConstraintLayout的使用,包括引入、相对位置、尺寸约束、宽高比、百分比宽高、位置偏向、权重、链、Guideline辅助线等关键特性,旨在帮助开发者更好地理解和应用ConstraintLayout,降低布局嵌套,提高性能。
4767

被折叠的 条评论
为什么被折叠?



