最近中毒很深,常常逛掘金,看到不少优秀的文章,感谢掘金。同时也看到不少标题,看看XXXX,一篇就够了。技术一直在不停的更新迭代,看一篇永远是不够的,建议再看一遍官网的,能够看到被做者过滤掉的信息或者最新的更新。这就是我为何会在文末放官网连接的缘由,若是有的话。android
ConstraintLayout做为一款能够灵活调整view位置和大小的Viewgroup被Google疯狂推荐,之前建立布局,默认根元素都是LinearLayout,如今是ConstraintLayout了。ConstraintLayout可以以支持库的形式最小支持到API 9,同时也在不断的丰富ConstraintLayout的API和功能。ConstraintLayout在复杂布局中可以有效的,下降布局的层级,提升性能,使用更加灵活。git
在app组件的Graldle默认都有以下依赖:github
//可能版本不同哦
implementation 'com.android.support.constraint:constraint-layout:1.1.3 复制代码
火烧眉毛想了解ConstraintLayout能在布局作点什么了。bash
相对定位,其实这跟RelativeLayout差很少,一个View相对另一个View的位置。app
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
...>
<TextView
...
android:text="Hello"
android:id="@+id/tvHello"/>
<TextView
...
android:text="World"
app:layout_constraintLeft_toRightOf="@+id/tvHello"/>
<TextView
...
android:text="GitCode"
app:layout_constraintTop_toBottomOf="@id/tvHello"/>
</android.support.constraint.ConstraintLayout>
复制代码
以TextView World相对位置属性layout_constraintLeft_toRightOf来讲,constraintLeft表示TextView World自己的左边,一个View有四条边,所以TextView的上、右、下边分别对应着constraintTop、constraintRight、constraintBottom。toRightOf则表示位于另一个View的右边,例如此处位于Hello的右边,所以对应还有toLeftOf、toRghtOf、toBottomOf,分别位于View Hello的左、右、下边。总结的说,constraintXXX表示View自身约束的边,toXXXOf表示另外一个View的边,而XXX的值能够是Left、Top、Right、Bottom,分别对应左,上、右、下边。layout_constraintStart_toEndOf
也是相似的道理。ide
另外须要注意的是,view的位置能够相对于同层的view和parent,在相对于parent的时候toLeftOf、toTopOf、toRghtOf、toBottomOf分别表示位于parent的内部左上右下边缘。如图:红色框表示parent view。工具
layout_constraintBaseline_toBaselineOf
属性。
<TextView
...
android:text="Hello"
android:id="@+id/tvHello"/>
<TextView
...
android:text="World"
app:layout_constraintBaseline_toBaselineOf="@id/tvHello"
app:layout_constraintLeft_toRightOf="@+id/tvHello"/>
复制代码
此时界面就如愿了,比Relativelayout方便多了。 布局
边距与日常使用并没有太大区别,但须要先肯定view的位置,边距才会生效。如:post
<TextView
...
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"/>
复制代码
在其余的ViewGroup,TextView的layout_marginTop
和layout_marginLeft
属性是会生效的,但在ConstraintLayout不会生效,由于此时TextView的位置还没肯定。下面的代码才会生效。性能
<TextView
...
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
复制代码
经常使用属性以下:
有时候,会有这种需求,在World可见的时候,GitCode与World的左边距是0,当World不见时,GitCode的左边距是某个特定的值。
World可见的效果,GitCode的左边距为0
在RelativeLayout居中,一般是使用如下三个属性:
而在ConstraintLayout居中则采用左右上下边来约束居中。
<TextView
...
android:text="Hello"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
复制代码
效果图:
那很简单,使用margin呀。不不不,这里要介绍的是另外两个属性,与LinearLayout的权重相似(固然,ConstraintLayout也可使用权重属性),但简单不少。
两个属性的取值范围在0-1。在水平偏移中,0表示最左,1表示最右;在垂直偏移,0表示最上,1表示最下;0.5表示中间。
<TextView
...
android:text="Hello"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
复制代码
效果:
圆形定位指的是View的中心点相对于另外View中心点的位置。贴张官网图。
吃个栗子:
<TextView
...
android:text="Hello"
android:id="@+id/tvHello"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:text="World"
app:layout_constraintCircle="@id/tvHello"
app:layout_constraintCircleRadius="180dp"
app:layout_constraintCircleAngle="135"/>
复制代码
效果图:Hello中间居中,World 135角度
ConstraintLayout的宽高设为WRAP_CONTENT
时,能够经过如下熟悉设置其最大最小尺寸。
在ConstraintLayout中控件能够三种方式来设置其尺寸约束。
WRAP_CONTENT
,内容自适配。MATCH_CONSTRAINT
,扩充可用空间。第一二种跟日常使用没什么区别。第三种会根据约束状况从新计算控件的大小。 在ConstraintLayout中,不推荐使用MATCH_PARENT
,而是推荐使用MATCH_CONSTRAINT
(0dp),它们的行为是相似的。
吃个栗子吧:
<TextView
android:text="Hello"
android:id="@+id/tvHello"
android:gravity="center"
android:padding="20dp"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/colorWhite"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content"/>
复制代码
设置layout_width
为0dp;layout_height
为wrap_content
;layout_marginRight
为20dp,与parent左右对齐。
效果图:
在1.1以前的版本,控件尺寸设为WRAP_CONTENT
,控件默认是由组件文本大小控制,其余约束是不生效的。能够经过如下属性设置是否生效。
控件设为MATCH_CONSTRAINT
时,控件的大小会扩展全部可用空间,在1.1版本后,能够经过如下属性改变控件的行为。
吃个栗子:
<TextView
android:text="Hello"
android:id="@+id/tvHello"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintWidth_default="percent"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
复制代码
将android:layout_width
设为MATCH_CONSTRAINT
,即0dp;将app:layout_constraintWidth_default
设为percent
;将app:layout_constraintWidth_percent
设为0.5,表示占parent的50%,取值范围是0-1。
效果图:
控件的宽高比,要求是宽或高至少一个设为0dp,而后设置属性layout_constraintDimensionRatio
便可。
<TextView
android:text="Hello"
app:layout_constraintDimensionRatio="3:1"
android:layout_width="0dp"
android:layout_height="100dp"
/>
复制代码
这里设置宽高比为3:1,高度为100dp,那么宽度将为300dp。
W
,
H
表示是宽高比仍是高宽比。以下面表示高宽比。
<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"/>
复制代码
链在水平或者垂直方向提供一组相似行为。如图所示能够理解为横向链。这里须要了解一点,A与parent的左边缘约束,B与parent的右边边缘约束,A右边和B左边之间相互约束,才能使用一条链。多个元素之间也是如此,最左最右与parent约束,元素之间边相互约束。否则下面的链式永远没法生效。
它两的值默承认以是
<?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">
<TextView
android:text="Hello"
android:id="@+id/tvHello"
android:gravity="center"
android:padding="20dp"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tvWorld"
android:textColor="@color/colorWhite"
android:background="@color/colorPrimaryDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="World"
android:gravity="center"
android:padding="20dp"
android:id="@+id/tvWorld"
app:layout_constraintLeft_toRightOf="@id/tvHello"
app:layout_constraintRight_toRightOf="parent"
android:textColor="@color/colorWhite"
android:background="@color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
复制代码
效果:
在链中,剩余空余空间默认平均给各元素,但有时能够经过权重属性layout_constraintVertical_weight
来指定分配空间的大小。
1.1以后,在链中使用边距时,边距是相加的,也就说,假设Hello的右边距为5,World的左边距为20,那么它们之间的边距就是25。在链式,边距先从剩余空间减去的,而后再用剩余的空间在元素之间进行定位。
在1.1以后,公开了优化器,经过在app:layout_optimizationLevel
来决定控件在哪方面进行优化。
参考线实际上不会在界面进行显示,只是方便在ConstraintLayout布局view时候作一个参考。
经过设置Guideline的属性orientation
来表示是水平方向仍是垂直方向的参考线,对应值为vertical
和horizontal
。能够经过三种方式来定位Guideline位置。
丢个栗子:
<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:layout_height="match_parent">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
android:orientation="vertical"
app:layout_constraintGuide_begin="10dp"/>
<Button android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintLeft_toLeftOf="@+id/guideline"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"/>
<Button android:text="Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
app:layout_constraintLeft_toLeftOf="@+id/guideline"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/button"/>
</android.support.constraint.ConstraintLayout>
复制代码
Guideline设置为垂直参考线,距离开始的位置为10dp。以下图所示,实际中须要把鼠标移到button才会显示出来哦。
Barrier有点相似Guideline,但Barrier会根据全部引用的控件尺寸的变化从新定位。例如经典的登陆界面,右边的EditText老是但愿与左右全部TextView的最长边缘靠齐。 若是两个TextView其中一个变得更长,EditText的位置都会跟这变化,这比使用RelativeLayout灵活不少。
<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">
<android.support.constraint.Barrier
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
android:id="@+id/barrier"
app:constraint_referenced_ids="tvPhone,tvPassword"
/>
<TextView android:layout_width="wrap_content"
android:text="手机号码"
android:id="@+id/tvPhone"
android:gravity="center_vertical|left"
android:padding="10dp"
android:layout_height="50dp"/>
<TextView android:layout_width="wrap_content"
android:text="密码"
android:padding="10dp"
android:gravity="center_vertical|left"
android:id="@+id/tvPassword"
app:layout_constraintTop_toBottomOf="@id/tvPhone"
android:layout_height="wrap_content"/>
<EditText android:layout_width="wrap_content"
android:hint="输入手机号码"
android:id="@+id/etPassword"
app:layout_constraintLeft_toLeftOf="@id/barrier"
android:layout_height="wrap_content"/>
<EditText android:layout_width="wrap_content"
android:hint="输入密码"
app:layout_constraintTop_toBottomOf="@id/etPassword"
app:layout_constraintLeft_toLeftOf="@id/barrier"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
复制代码
app:barrierDirection
所引用控件对齐的位置,可设置的值有:bottom、end、left、right、start、top.constraint_referenced_ids
为所引用的控件,例如这里的tvPhone,tvPasswrod。
用来控制一组view的可见性,若是view被多个Group控制,则以最后的Group定义的可见性为主。
吃个香喷喷栗子吧: Group默承认见时,是这样的。
visible
属性为gone.
<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">
<android.support.constraint.Group
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/group"
android:visibility="gone"
app:constraint_referenced_ids="tvPhone,tvPassword"
/>
<TextView android:layout_width="wrap_content"
android:text="手机号码"
android:id="@+id/tvPhone"
android:gravity="center_vertical|left"
android:padding="10dp"
android:layout_height="50dp"/>
<TextView android:layout_width="wrap_content"
android:text="密码"
android:padding="10dp"
android:gravity="center_vertical|left"
android:id="@+id/tvPassword"
app:layout_constraintLeft_toRightOf="@id/tvPhone"
app:layout_constraintTop_toBottomOf="@id/tvPhone"
android:layout_height="wrap_content"/>
<TextView android:layout_width="wrap_content"
android:text="GitCode"
android:padding="10dp"
android:gravity="center_vertical|left"
app:layout_constraintLeft_toRightOf="@id/tvPassword"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
复制代码
效果就变成了这样了,tvPhone,tvPassword都被隐藏了。
一个view占位的占位符,当指定Placeholder的content属性为另外一个view的id时,该view会移动到Placeholder的位置。
代码中,将TextView的定位在屏幕中间,随着将id设置给Placeholder的属性后,TextView的位置就跑到Placeholder所在的地方,效果图跟上图一直。
<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">
<android.support.constraint.Placeholder
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:content="@id/tvGitCode"
/>
<TextView android:layout_width="wrap_content"
android:text="GitCode"
android:id="@+id/tvGitCode"
android:padding="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:gravity="center_vertical|left"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
复制代码
在2.0,为ConstraintLayout增长了ConstraintProperties、ConstraintsChangedListener等,感兴趣能够本身看看官网。
在写本文以前,其实还不会用ConstraintLayout,写完本文以后,已经上手和喜欢上了,知足本身在实际开发中想要的效果,可以有效的减小布局的层级,从而提升性能。不知道看完本文,你会使用ConstraintLayout了没有?