Google I/O 2016 上发布了 ConstraintLayout,据说很强大,那就一探究竟吧!

gradle配置

      compile 'com.android.support.constraint:constraint-layout:1.0.0-beta2'  
  • 1
  • 1

阅读前提:熟悉四大基础布局

一、位置控制

  • 8个边界控制属性 
    注:最左边表示可移动的最左边,左边表示View的左边边界
    app:layout_constraintLeft_toLeftOf
app:layout_constraintLeft_toRightOf 我最左边的位置 在别人的右边 下面的意思类似
app:layout_constraintRight_toRightOf
app:layout_constraintRight_toLeftOf
app:layout_constraintTop_toTopOf
app:layout_constraintTop_toBottomOf
app:layout_constraintBottom_toBottomOf
app:layout_constraintBottom_toTopOf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

不明白没关系,看例子。

* 例如:

    <!--如图,左边一个A,右边一个C,我如果想新建一个B在A C之间,如下-->
<Button
app:layout_constraintLeft_toRightOf="@+id/bt_a"
app:layout_constraintRight_toLeftOf="@+id/bt_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"/>
<!--字面理解:1.我最左边的位置,在button A的右边-->
<!--字面理解:1.我最右边的位置,在button C的左边-->
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

如上图中,最左边和最右边的位置已经确定,B出现在A和C中间,但是如果我不想在中间怎么办(比如说我想靠右一点)

  • 这里引入2个偏移属性
    layout_constraintHorizontal_bias(水平方向偏移)(范围0-1)
layout_constraintVertical_bias(垂直方向偏移)(范围0-1)
  • 1
  • 2
  • 1
  • 2

怎么理解?我只发图不说话,看图 
如图:B的水平偏移为0 

    app:layout_constraintHorizontal_bias="0"
  • 1
  • 1

如图:B的水平偏移为0.5 

    app:layout_constraintHorizontal_bias="0.5"
  • 1
  • 1

如图:B的水平偏移为0.7 

    app:layout_constraintHorizontal_bias="0.7"
  • 1
  • 1

如图:B的水平偏移为1 

    app:layout_constraintHorizontal_bias="1"    
  • 1
  • 1

总结:(明白了吗?不明白请继续看图), 
1.通过8个边界约束属性可以固定View的最左边、最右边、最上面、最下面的位置 
2.通过设置偏移属性,可以控制View在边界范围移动,最左边是0,最右边是1,中间是0.5 
3.当设置了边界约束属性后,View会自动出现在中间,也就是说,默认的偏移属性是0.5

二、大小控制

  • 先介绍两个布局大小控制属性
    layout_constraintHorizontal_weight //水平方向上比重,类似线性布局
layout_constraintVertical_weight //垂直方向上比重,类似线性布局
  • 1
  • 2
  • 1
  • 2

下面我将用ConstraintLayout来模仿一个水平方向的线性布局的例子

完整布局文件:

    <?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"
tools:ignore="MissingConstraints">
<!--A边界控制属性 有 左 和 右-->
<Button
android:id="@+id/bt_a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="A"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/bt_b"/>
<!--B边界控制属性 也有 左 和 右-->
<Button
android:id="@+id/bt_b"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="B"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@id/bt_a"
app:layout_constraintRight_toLeftOf="@id/bt_c"/>
<!--C边界控制属性 只有右-->
<Button
android:id="@+id/bt_c"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="C"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

效果如下(效果图中C明显比较小,说明C 比重设置没有效果) 

结论: 
1.实现水平方向线性布局,所有的View都必须设置左右边界控制属性,而且相互控制 
2.实现比重大小控制,必须设置layout_width=”0dp”

如图布局(能看懂基本上说明你已经掌握了比重控制)

    <?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"
tools:ignore="MissingConstraints">
<TextView
android:background="#0f0"
android:id="@+id/bt_a"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="A"
app:layout_constraintBottom_toTopOf="@id/bt_b"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/bt_b"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1"/>
<TextView
android:background="#0f0"
android:id="@+id/bt_b"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="B"
app:layout_constraintBottom_toTopOf="@id/bt_c"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@id/bt_a"
app:layout_constraintRight_toLeftOf="@id/bt_c"
app:layout_constraintTop_toBottomOf="@id/bt_a"
app:layout_constraintVertical_weight="1"/>
<TextView
android:background="#0f0"
android:id="@+id/bt_c"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="C"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@id/bt_b"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/bt_b"
app:layout_constraintVertical_weight="1"/>
</android.support.constraint.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

效果图如下: 

三、位置控制补充

绝对坐标:父布局左上角默认为(0,0),这个坐标是是相对于父布局左上角的坐标

    layout_editor_absoluteX 绝对坐标x
layout_editor_absoluteY 绝对坐标y
  • 1
  • 2
  • 1
  • 2

当设置了左边界控制属性,x绝对坐标失效,请使用基础布局的(layout_marginLeft替代) 
当设置了上边界控制属性,y绝对坐标失效,请使用基础布局的(layout_marginTop替代)

因此,,绝对坐标。。不好适配,也不好控制。差评。

另外附上一张 
Android Studio 超智能的控制设置图,如下

 
4

0
 
 
 
猜你在找

Android基础之布局ConstraintLayout的更多相关文章

  1. 将Android Studio默认布局ConstraintLayout切换成LinearLayout

    将Android Studio默认布局ConstraintLayout切换成LinearLayout     大部分人初次使用google android 扁平化布局ConstraintLayout都 ...

  2. Android 基础:常用布局 介绍 & 使用(附 属性查询)

    Android 基础:常用布局 介绍 & 使用(附 属性查询)   前言 在 Android开发中,绘制UI时常需各种布局 今天,我将全面介绍Android开发中最常用的五大布局 含 Andr ...

  3. Android 基础 (四大组件,五大存储,六大布局)

    Android四大组件: 参考:https://blog.csdn.net/shenggaofei/article/details/52450668 Android四大组件分别为activity.se ...

  4. 基础4 Android基础

    基础4 Android基础 1. Activity与Fragment的生命周期. Activity生命周期 打开应用 onCreate()->onStart()->onResume 按BA ...

  5. Android中的布局优化方法

    http://blog.csdn.net/rwecho/article/details/8951009 Android开发中的布局很重要吗?那是当然.一切的显示样式都是由这个布局决定的,你说能不重要吗 ...

  6. Android基础总结(8)——服务

    服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...

  7. Android基础-系统架构分析,环境搭建,下载Android Studio,AndroidDevTools,Git使用教程,Github入门,界面设计介绍

    系统架构分析 Android体系结构 安卓结构有四大层,五个部分,Android分四层为: 应用层(Applications),应用框架层(Application Framework),系统运行层(L ...

  8. Android基础新手教程——1.10 反编译APK获代替码&amp;资源

    Android基础新手教程--1.10 反编译APK获代替码&资源 标签(空格分隔): Android基础新手教程 本节引言: "反编译Apk".看上去好像好像非常高端的样 ...

  9. Android逆向系列文章— Android基础逆向(6)

    本文作者:HAI_ 0×00 前言 不知所以然,请看 Android逆向-Android基础逆向(1) Android逆向-Android基础逆向(2) Android逆向-Android基础逆向(2 ...

随机推荐

  1. js之放大镜效果

      HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  2. matlab 相关系数的计算

    1. 首先说说自相关和互相关的概念.     这 个是信号分析里的概念,他们分别表示的是两个时间序列之间和同一个时间序列在任意两个不同时刻的取值之间的相关程度,即互相关函数是描述随机信号 x(t),y ...

  3. ios逆向工程-动态分析

    先说说为什么要分析应用吧,如果你想从一个ios应用中获取有用的信息,或者你想修改该应用的一些功能,前提当然是要先知道该app的逻辑和结构了. 动态分享工具比较少,我们先分析个简单的,全民工具Cycri ...

  4. sublime忽略打开工程中某些文件夹,不在搜索之列

    { "folders": [ { "follow_symlinks": true, "path": ".", " ...

  5. HDU - 6521 Party (SYSU校赛K题)(线段树)

    题目链接 题意:n个人排成一列,一开始他们互不认识,每次选[l,r]上的人开party,使他们互相认识,求出每次party之后新互相认识的人的对数. 思路:把“互相认识”变成单向连边,只考虑左边的人对 ...

  6. spring @Autowired注入的原理

    只知道如何用Autowired注解,知道可以替代set,get方法,很方便,却一直不知道,为什么可以代替 今天探索一下原因,所谓知其然还要知其所以然,才能理解的更好,记忆的更牢,才能转化为自己的知识. ...

  7. Loj 538 递推数列

    Loj 538 递推数列 出题人:这题提高难度吧.于是放在了%你赛的 \(D1T2\) . 递推式为 \(a_i=k*a_{i-1}+a_{i-2}\) , 注意到 \(k\in \mathbb{N_ ...

  8. Java--数组转成list,list转数组

    数组转成list: 方法一: String[] userid = {"aa","bb","cc"}; List<String> ...

  9. Mac OS上搭建LNMP开发环境

    1. 概述 LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构.Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统.代表版本有:debian.c ...

  10. Siddhi cep java 集成简单使用

    Siddhi 是一个开源的cep (Complex Event Processing)类库,有一个明显的例子是uber 的事件处理,具体可以google 几张参考cep 以及siddhi 图 java ...