ConstraintLayout你可能要了解的一些内容

banner.jpg

好久没有写文章了!以至于我都不知道该写什么了?最近项目中对ConstraintLayout的使用明显增多了!所以今天和大家分享一下关于ConstraintLayout的一些你应该了解的内容!

本文知识点

  • ConstraintLayout的一些相应的属性及展示;
    1. 约束的相关属性
    2. 边距相关的属性
    3. 位置相关的属性
    4. 链相关的属性
    5. 辅助线相关的属性
    6. 一些不知怎么归类的属性
  • ConstraintLayout布局的时候应该注意的一些内容;
    1. 关于ID的问题
    2. 关于设置"0dp"的问题
    3. 关于屏幕适配的问题

1.ConstraintLayout的一些相应的属性及展示

关于这个控件我想大家应该都见过,只不过是你没有注意过吧!如果你的Android Studio的版本在3.0以上的话,那么跟布局一般都是ConstraintLayout,以前一般的做法都是,直接替换成相应的布局,直到有一天,我突然发现这个控件如此之强大,强大到我都不再想用其它控件了!但是强烈建议你们使用代码去写!不要使用拖拽的那种方式,因为我觉得每次拖拽的时候我的电脑卡的不行!这个理由是不是很牵强~~~哈哈!

关于ConstraintLayout属性,我准备分成几类进行讲解

  • 1.约束的相关属性
  • 2.边距相关的属性
  • 3.位置相关的属性
  • 4.链相关的属性
  • 5.辅助线相关的属性
  • 6.一些不知怎么归类的属性

1.1 约束的相关属性

  • 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

这里和你说一个简单方便记忆的方法,XXX_toXXXOf前面的是你现在的控件,后面的XXX是你要依赖的控件,拿layout_constraintLeft_toLeftOf说明一下:

<?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:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1按钮1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <Button
        app:layout_constraintTop_toBottomOf="@id/btn1"
        app:layout_constraintLeft_toLeftOf="@id/btn1"
        app:layout_constraintRight_toRightOf="@id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2" />

</android.support.constraint.ConstraintLayout>
效果图
  1. 先主要看按钮1的约束条件,按钮1的左边以父控件的左边为约束条件,右边以父控件的右边为约束条件,这个时候,会有相应的居中显示(也就是说,如果你左右都设置了约束条件的话,那么如果有相应的空间,那么它会居中显示)!

  2. 在看按钮2的约束条件,按钮2的左边以按钮1的左边为约束条件,按钮2的右边以按钮1的右边为约束条件,这里注意一点,如果两个控件不一样大的时候,会以中心线对齐!

基本上就这么多,没有什么好说的。这里建议大家使用看一下效果,使用多了自然就知道效果了!

1.2 边距相关的属性

  • android:layout_marginStart
  • android:layout_marginEnd
  • android:layout_marginLeft
  • android:layout_marginTop
  • android:layout_marginRight
  • android:layout_marginBottom
  • layout_goneMarginStart
  • layout_goneMarginEnd
  • layout_goneMarginLeft
  • layout_goneMarginTop
  • layout_goneMarginRight
  • layout_goneMarginBottom

这里的边距和之前的边距是一样的,但是layout_goneMarginXXX这个属性是当位置约束目标的可见性为View.GONE,您还可以使用以下属性指示要使用的不同边距值。这里说明一下:如果你约束的控件GONE了之后,是以左上开始计算距离的,剩下的试一下你就知道了!

1.3 位置相关的属性

首先我真的不知道,这么分这些属性是否正确,所以只要看相关的属性就可以了!

1.3.1 百分比的属性

  • layout_constraintHorizontal_bias 水平的百分比
  • layout_constraintVertical_bias 垂直的百分比
<?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:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1按钮1"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>
效果图

这里的layout_constraintXXX_bias相当于一个百分比也就是距离的百分比,像上面的的就是距离左边30%那么右边的就是70%了!

  • app:layout_constraintDimensionRatio="1:1" 设置相应的宽高比
<?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:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="按钮1"
        app:layout_constrainedWidth="true"
        app:layout_constraintDimensionRatio="1:1" />
</android.support.constraint.ConstraintLayout>
效果图

上面是宽高比是"1:1",这里默认是宽高比的!

如果你像设置高度比这样写就可以了"h,2:1"但是","别千万别忘了

效果图

1.3.2 圆角半径的一些属性

  • layout_constraintCircle :引用另一个小部件ID
  • layout_constraintCircleRadius :到其他小部件中心的距离
  • layout_constraintCircleAngle :小部件应该处于哪个角度(以度为单位,从0到360)
<?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:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        app:layout_constraintCircle="@id/btn1"
        app:layout_constraintCircleAngle="135"
        app:layout_constraintCircleRadius="100dp" />
</android.support.constraint.ConstraintLayout>
效果图

这里为了更好的说明,所以我准备在用两张图说明一下:

<center class="half">
<img src="https://user-gold-cdn.xitu.io/2018/7/31/164efc6d602a783e?w=325&h=325&f=png&s=8841"/>
<img src="https://user-gold-cdn.xitu.io/2018/7/31/164efc6efca95ff6?w=325&h=325&f=png&s=8659"/>
</center>

首先,角度是逆时针计算的,半径是以图形的中心进行计算的!剩下的你写一遍基本上就知道了!!!

1.3.3 限制的一些属性

  • android:minWidth 设置布局的最小宽度
  • android:minHeight 设置布局的最小高度
  • android:maxWidth 设置布局的最大宽度
  • android:maxHeight 设置布局的最大高度

这些属性没有什么说的,和之前一样!

1.4 链相关的属性

关于链表的属性很有意思,我个人理解他是LinearLayout中weight的加强版!我们先说说简单的实现吧!为什么叫做链呢?因为所有的同方向的约束组成了一个链式结构!

  • layout_constraintHorizontal_chainStyle
  • layout_constraintVertical_chainStyle
<?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:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        app:layout_constrainedWidth="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/btn2" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        app:layout_constrainedWidth="true"
        app:layout_constraintLeft_toRightOf="@id/btn1"
        app:layout_constraintRight_toLeftOf="@id/btn3" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        app:layout_constrainedWidth="true"
        app:layout_constraintLeft_toRightOf="@id/btn2"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
效果图

这里要注意一个相应的问题,怎么理解上面我说的链式结构呢?仔细看的童鞋,在上面这个例子中可能会发现,每一个控件左右的都有相应的约束,所以才叫链表式的结构!

效果图

这张图完全能说明我上面说的内容!

  • layout_constraintHorizontal_weight
  • layout_constraintVertical_weight

这里既然说到LinearLayout就在这里说一下这两个属性吧!因为这两个属性和weight属性差不多,基本上的用法是一样的

<?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:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮1"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/btn2" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮2"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintLeft_toRightOf="@id/btn1"
        app:layout_constraintRight_toLeftOf="@id/btn3" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮3"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_weight="3"
        app:layout_constraintLeft_toRightOf="@id/btn2"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

这里就是设置了相应的一些weight的属性!效果图就变成了这个样子!!!

效果图

在说说上面的两个属性,关于这两个属性都差不多,只是一个是约束水平的一个是约束垂直的,还是用一张图说明一下,看一眼效果你就知道了!!!

效果图

这张效果图,说明了chainStyle可以设置的一些属性!关于链式结构基本上注意的内容就这么多!!!也都是平时项目上能用到的!但是千万要注意一点,必须左右都有相应的约束!否则不会有这种效果

"三遍" 重要的事情说"三遍" 哈哈!!!

1.5 辅助线相关的属性

在ConstraintLayout中有一个辅助线的概念,它是不会显示在页面上的,只是一条你假象的线,然后你可以以此为约束进行布局!

  • android:orientation 辅助线的方向
  • app:layout_constraintGuide_begin 距离顶部的距离
  • app:layout_constraintGuide_end 距离底部的距离
  • app:layout_constraintGuide_percent 相当于父容器的百分比
<android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="100dp" />
效果图

可以看出,Guideline计算的距离都是依靠父容器的距离进行约束的,所以这里你即使设置了相应的控件约束,其实是不好使的!我试过,但是如果你真的像以控件进行约束也不是不可以的,我的做法是:直接把控件的大小写出来,这样不用上面的三个距离的尺寸就可以进行相应的布局了,虽然这样有点傻!剩下两个你试一下就可以了!图就不往上贴了!!!

1.6 一些不知怎么归类的属性

  • layout_constraintBaseline_toBaselineOf 基线对其
<?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:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="按钮1" />

    <Button
        android:id="@+id/btn2"
        app:layout_constraintBaseline_toBaselineOf="@id/btn1"
        app:layout_constraintLeft_toRightOf="@id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:text="按钮2" />
</android.support.constraint.ConstraintLayout>
效果图
  • layout_constraintWidth_percent
  • layout_constraintHeight_percent

宽度和高度占父控件的百分比,其中的值是0~1的!

<?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:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintHeight_percent="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="按钮1" />
    
</android.support.constraint.ConstraintLayout>
效果图

其实就相当于占用父容器的百分比,上面就相当于50%

2. ConstraintLayout布局的时候应该注意的一些内容

2.1 关于ID的问题

其实我也不是到是Android Studio还是SDK的版本影响,但是有的时候在使用链式结构的时候,会出现控件找不到的现象?为什么呢?因为在使用链式结构的时候,前面往往使用后面的控件ID,但是有的时候就不会出现这种状况,后来,我找到了一种解决方法,就是在前面引用的时候这样写"@+id/xxx",乍一看没有什么,但是其实多了一个"+"号,这个"+"号的作用是在没有发现这个控件ID的时候,主动创建一个相应的ID!就酱紫。。。

2.2 关于设置"0dp"的问题

其实在不涉及到左右或者上下比例的情况下,只要你在相应方向都有约束的情况,那么相应的方向上设置"0dp"的时候,它会直接铺满全屏的!但是如果涉及到左右或者上下比例的情况下,就需要设置相应的layout_constraintXXX_weight了!

2.3 关于屏幕适配的问题

都知道Android屏幕适配真的是一件非常头疼的事情,其实这个控件能帮我们解决相当大的一部分问题;一般我的做法是使用layout_constraintXXX_percent直接把控件按照百分比进行相应的计算!能解决一部分的适配问题,但是内部的自己没有办法解决,需要你自己去解决!!!!

基本上知道了上面这些,对于这个控件的使用就可以非常轻松了!!!不知道还有没有什么落下的,如果有落下的我想起来在进行补充,如果你有什么问题也可以给我留言,我看到了一定及时回复你!!!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,667评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,361评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,700评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,027评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,988评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,230评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,705评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,366评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,496评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,405评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,453评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,725评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,803评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,015评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,514评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,111评论 2 341

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,287评论 25 707
  • ConstraintLayout,让布局更优雅。 一、为什么要用ConstraintLayout 上图是网易100...
    宇是我阅读 15,128评论 21 161
  • ConstraintLayout 是在 2016 年 Google 大会上推出的一个新的布局控件,众所周知,Con...
    lijiankun24阅读 5,075评论 3 33
  • 今天再次重新带爱人的老妈去石板岩看病,上周本来就计划带她去的,结果她赖着坚决不起床,怎么说也不管用,但又答应了捎别...
    甲午之印阅读 75评论 0 0
  • 小时候 爱情是一封羞涩的信 我藏在信的这头 你躲在信的那头 长大了 爱情是一座神奇的迷宫 我刚到入口 你却已离开了...
    千幻风阅读 171评论 0 0