本章节将分为两个模块给你们推送,以为不错的能够关注下哦php
Android ConstraintLayout是谷歌推出替代PrecentLayout的组件。android
支持相对布局、线性布局、帧布局,看来更像是FrameLayout 、LinearLayout、`RelativeLayout·三者的结合体,而且比这三者更强大的是实现了百分比布局。bash
你们都知道安卓碎片严重,使用百分比适配,那么将完全解决适配问题app
总结:我最近也是刚学,学完以后,发现这个布局已经将上述的全部布局的特色所有融合在一块儿了,使用起来简单方便的不要不要的,就是学习的属性有点多啊。ide
不过,多也是正常的,毕竟融合了五大布局的全部特色,学完这个布局,各类界面UI都难不倒咱们了布局
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
复制代码
我使用的是Android Studio3.0.1版本,已经自动导入了,默认使用的就是这个布局学习
咱们先了解一下一些基本的概念ui
注意,这里的属性都得使用命名空间来才能使用
复制代码
宽高属性与以前的layout相同,wrap_content
和match_parent
,但除此以外,还多出了一种,名为match contraint
spa
实际上,这个多出来的至关于0dp
,若是以前使用过LinearLayout
的权重的话,应该对0dp
有印象.3d
这里,约束布局应该是继承了Linearlayout
的特性,以后咱们设置权重与Linearlayout
的操做也是相似,具体的请日后面看,这但是实现了百分比布局的强大布局。
layout_constraintLeft_toLeftOf
当前控件的左边依赖于属性控件的左边
layout_constraintLeft_toRightOf
当前控件的左边依赖于属性控件的右边
layout_constraintRight_toLeftOf
ayout_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
示例:
<?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:layout_height="match_parent">
<Button android:id="@+id/btnA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/>
<Button android:id="@+id/btnB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toEndOf="@id/btnA"/>
</android.support.constraint.ConstraintLayout>
复制代码
A左边依赖总布局的左边,顶部则是依赖总布局的顶部,B则是左边依赖于A的右边,顶部依赖父布局的顶部
其余的就不一一列举了,触类旁通,挺容易的
只有在设置了依赖,以后设置边距才会效果
这个属性不须要使用app开头,属于本来的属性
android:layout_marginStart
设置左边的边距
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
例如:
<?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:layout_height="match_parent">
<Button android:id="@+id/btnA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A" android:layout_marginLeft="30dp" android:layout_marginTop="50dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/>
<Button android:id="@+id/btnB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="60dp" android:text="B" app:layout_constraintStart_toEndOf="@id/btnA"/>
</android.support.constraint.ConstraintLayout>
复制代码
示例:
使控件B与A垂直对齐
B的左边依赖A的左边,B的右边依赖A的右边便可
<?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:layout_height="match_parent">
<Button android:id="@+id/btnA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="124dp" android:layout_marginTop="16dp" android:text="A" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/>
<Button android:id="@+id/btnB" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginTop="8dp" android:text="B" app:layout_constraintEnd_toEndOf="@+id/btnA" app:layout_constraintStart_toStartOf="@+id/btnA" app:layout_constraintTop_toBottomOf="@+id/btnA"/>
</android.support.constraint.ConstraintLayout>
复制代码
<android.support.constraint.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="TextView1,TextView2" />
复制代码
app:barrierDirection为屏障所在的位置,可设置的值有:bottom、end、left、right、start、top
app:constraint_referenced_ids为屏障引用的控件,可设置多个(用“,”隔开)
Group能够把多个控件归为一组,方便隐藏或显示一组控件,举个例子:
<android.support.constraint.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:constraint_referenced_ids="TextView1,TextView3" />
复制代码
设置好占位符控件的位置,以后调用setContent,把指定id的控件放在占位符的位置
app:content=
可使用这个控件达到百分比布局的效果,或者是当前的控件没有符合条件的参照物的状况使用
Guideline
还有三个重要的属性,每一个Guideline
只能指定其中一个:
layout_constraintGuide_begin
,指定左侧或顶部的固定距离,如100dp,在距离左侧或者顶部100dp的位置会出现一条辅助线
layout_constraintGuide_end
,指定右侧或底部的固定距离,如30dp,在距离右侧或底部30dp的位置会出现一条辅助线
layout_constraintGuide_percent
,指定在父控件中的宽度或高度的百分比,如0.8,表示距离顶部或者左侧的80%的距离。
android:orientation
设置垂直或者是水平
Guideline
是隐藏的,不用咱们进行多余的设置,虽然外面在预览面板能够死看到它的存在
例子:使一个按钮的长度占满屏幕一半
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guidelineBegin"
app:layout_constraintGuide_percent="0.5"
android:orientation="vertical"/>
<Button
android:text="Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guidelineBegin"
app:layout_constraintTop_toTopOf="parent" />
复制代码
方便快捷调整控件的宽高比,结合Guideline使用更佳
例子:
app:layout_constraintDimensionRatio
想要宽度与总布局同样,但高度是宽度的三分之一
宽高比为3:1
<Button android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintDimensionRatio="3:1"/>
复制代码
能够把这个当作成一个增强版的LinearLayout
chainstyle
有三种属性,分别是spread
,spread_inside
,pack
,效果以下图
spread
元素将展开(默认)
spread_inside
元素展开,但链的端点不会展开
pack
链中的元素将包裹在一块儿。子控件的水平或垂直方向的偏置bias
属性会影响包裹中元素的位置
weighted chain
是在spread chain
的基础上,而packed chain with bias
则是在weight chain
的基础上
style为spread
的,使用layout_constraintHorizontal_weight
或layout_constraintVertical_weight
来设置weigh
权重
style为pack
的,使用layout_constraintHorizontal_bias
或layout_constraintVertical_bias
进行偏移设置,属性值0-1,也是百分比,改第一个元素
这里须要说明的是,除了水平,还能够是垂直排列,不过,不能经过属性来更改,而是经过约束来更改,把左边改成顶端,右边改成底部
若是是水平的,使用属性layout_constraintHorizontal_chainStyle
进行chainstyle属性的更改
垂直的则是使用layout_constraintVertical_chainStyle
例子:
三个按钮平分宽度(只须要将宽度设置为0dp
就能够达到目的)
<Button
android:id="@+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintEnd_toStartOf="@id/btn1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintStart_toEndOf="@id/btn"
app:layout_constraintEnd_toStartOf="@id/btn2"/>
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintStart_toEndOf="@id/btn1"
app:layout_constraintEnd_toEndOf="parent"/>
复制代码