ConstraintLayout使用

ConstraintLayout约束布局

Google大力推广布局方式。
高性能:基本一层布局搞定,不用嵌套多层布局
功能强大:百分比布局、设置自身宽高比例,各种辅助组件
可拖拽编辑:和ios拖拽布局很相似
缺点:用了约束布局后,再也无法回到以前的布局方式了,重度依赖

基本布局方式

app:layout_constraintStart_toStartOf -->自身左边与目标左边对齐

偏移量bias

居中显示,要往左边靠一点。可以通过bias来实现

app:layout_constraintHorizontal_bias-->水平方向的偏移,取值[0f,1.0f](0为靠向最左边,1为靠到最右边)

margin

margin生效,必须同一方向上的约束,要marginBottom就必须layout_constraintBottom_toxxxof

goneMargin(隐藏边距)

-layout_goneMarginStart
-layout_goneMarginEnd
-layout_goneMarginLeft
-layout_goneMarginTop
-layout_goneMarginRight
-layout_goneMarginBottom

大小设置

约束布局下三种大小设置模式

spread, wrap,percent;

app:layout_constraintWith_default-->设置

注意:使用约束布局的大小设置,需要先将要设置的宽或高指定0dp

spread:默认

表示在约束条件下的最大尺寸

wrap模式

自适应大小,不会超过约束条件下的最大尺寸
传统的wrap_content模式,约束布局有额外的属性,使得它也能保留约束条件

android:layout_width="0dp"// 对应比例的地方为0
app:layout_constrainedWidth="true"此时与wrap模式一样的效果
###percent模式
以父布局的百分比作为自身大小,通过layout_constraintWith_percent设置比例大小[0f,1.0f]

指定宽高比ratio

使其生效必须设置宽或高为0dp

app:layout_constraintDimensionRatio-->指定view自身的宽高比例。

如果宽高两项为0dp的话,则最终尺寸设置符合约束的最大尺寸,同时保持设置的比例。有时候不是我们想要的,可以指定宽或者高(H,W)哪一边约束条件来确定尺寸。

app:layout_constraintDimensionRatio="W,2:1"-->宽对按照比例计算最终尺寸。
app:layout_constraintDimensionRatio="H,2:1"-->则相反

最大和最小值(max/min)

app:layout_constraintHeight_min

链布局(chain)

可以快速实现等分布局,还可以实现类似LinearLayout布局的weight比重功能。
约束链三种模式:
spread:view之间均匀分布(默认)
spread_inside:除了约束链的头部和尾部贴在两边,其余均匀分布
packed:所有view紧贴在一起,默认居中

注意:layout_constraintLeft_toLeftOf(正常)和layout_constraintStart_toStartOf(不正常)在布局表现不一样;以此类推

image.png

在链上设置权重

app:layout_constraintHorizontal_weight--->权重

圆形布局

app:layout_constraintCircle-->圆心,某个view的id
app:layout_constraintCircleRadius-->半径
app:layout_constraintCircleAngle-->角度,值[0,360],0是正上方

button2在button1正上方(半径100dp)向右偏移45度

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        tools:context=".MainActivity">
   <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button ONE"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button 
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintCircle="@+id/button1"
            app:layout_constraintCircleRadius="100dp"
            app:layout_constraintCircleAngle="45"
            android:text="Button TWO"/>
</android.support.constraint.ConstraintLayout>

bt2在bt1右下侧

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <Button
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constrainedWidth="false"
            android:text="button one"/>

    <Button
            android:id="@+id/bt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toRightOf="@+id/bt1"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/bt1"
            android:text="Button TWO"/>

</android.support.constraint.ConstraintLayout>

app:layout_constrainedWidth="false" 时候随着bt2内容的增多,bt2横向变宽当宽度达到最大时,自动换行纵向变高,bt2会扩展到bt1中心位置下方,bt2屏幕右侧会覆盖一部分内容。
app:layout_constrainedWidth="true" bt2会扩展到bt1右侧位置下方,屏幕右侧不会覆盖一部分内容。

辅助组件

Group:控制可见
Guideline:辅助线
Barrier:边界范围

Group控制可见性

Group虚拟视图,通过constraint_refrenfed_ids放置里面,统一同时控制这些view的课件性。

<androidx.constraintlayout.widget.Group
        android:id="@+id/mGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="img1,img2"
        />
// 代码中控制可见性
mGroup.visibility = View.VISIBLE

辅助线

Guideline虚拟辅助线,水平、垂直,帮助定位。
两个view在屏幕中间一左一右,可以通过在屏幕中间放置一个虚拟辅助线,两个view分布约束到辅助线的两侧。

android:orientation-->设置垂直还是水平
app:layout_constraintGuide_percent-->通过百分比设置位置,取值[0f,1.0f]或者[0%,100%]
app:layout_constraintGuide_begin-->设置相对start/top的偏移量,dp
app:layout_constraintGuide_end-->设置相对end/bottom的偏移量,dp

<androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"
        />

    <ImageView
        android:id="@+id/img1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/img1"
        android:scaleType="centerCrop"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="50dp"
        />

    <ImageView
        android:id="@+id/img2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/img2"
        android:scaleType="centerCrop"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@+id/guideline"
        android:layout_marginTop="20dp"
        android:layout_marginStart="50dp"
        />

Barrier:获取边界范围

Barrier可以获取多个约束view的边界,可以获得所包含的多个view的最左最右等边界。
左边是text1和text2,另外一个view必须放在这两个text的右边。barrier可以动态获取右侧。解决问题

<androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="text1,text2"
        app:barrierDirection="end"
        />
    <ImageView
        android:id="@+id/img1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/img1"
        android:scaleType="centerCrop"
        app:layout_constraintTop_toTopOf="@id/text1"
        app:layout_constraintBottom_toBottomOf="@id/text2"
        app:layout_constraintStart_toEndOf="@id/barrier"
        android:layout_marginStart="50dp"
        />

技巧 宽度限制layout_width="0dp"(高度上同理)

场景1:设置view2的宽度和view1的宽度一样。设置view2为
android:layout_width="0dp"
layout_constraintStart_toStartOf=view1;
layout_constraintEnd_toEndOf=view1;

场景2:设置百分比宽为0.7:
android:layout_width="0dp"
layout_constraintWidth_percent="0.7"

merge 在ConstraintLayout使用

主布局activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity2">
    <TextView
        android:id="@+id/tvTest1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="50dp"
        android:text="测试布局"
        android:textSize="26sp"
        />
    <include layout="@layout/include_test"/>
</androidx.constraintlayout.widget.ConstraintLayout>

次布局:include_test.xml

<?xml version="1.0" encoding="utf-8"?>
<merge 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"
    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
    <TextView
        android:id="@+id/tvTest2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/tvTest1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="测试布局"
        android:textSize="26sp"
        />
</merge>

注意:
主布局的include不需要加id和宽高限制。
次布局include中的merge不需要宽高限制。需要添加tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"约束,方便在xml预览主视图。
merge中的布局可以直接引用主视图的布局进行位置定位。

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

推荐阅读更多精彩内容