Android 约束布局 ConstraintLayout学习

一、居中

<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:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="hello"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

app:layout_constraintBottom_toBottomOf="@id/layout_a"  底部与控件layout_a的底部对齐

app:layout_constraintEnd_toEndOf="parent"                          右侧父布局的右侧对齐

app:layout_constraintStart_toStartOf="parent"                      左侧

app:layout_constraintTop_toTopOf="parent"                          顶部




二、横向左侧占比,纵向顶部占比

<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:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="hello"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintHorizontal_bias="0.25"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintVertical_bias="0.25" />

</android.support.constraint.ConstraintLayout>

app:layout_constraintHorizontal_bias="0.25"    横向占比  控件距左侧距离加上控件距右侧距离,进行4等分,左侧占一份

app:layout_constraintVertical_bias="0.25"        纵向占比  控件距顶部距离加上控件距底部距离,进行4等分,顶部占一份


三、辅助线Guideline

<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.Guideline

        android:id="@+id/guide_line"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        app:layout_constraintGuide_percent="0.5" />

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginBottom="40dp"

        android:text="hellohellohellohellohello"

        app:layout_constraintBottom_toTopOf="@id/guide_line"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>


四、chainStyle

<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:id="@+id/tv_a"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="a"

        app:layout_constraintHorizontal_chainStyle="packed"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintEnd_toStartOf="@+id/tv_b" />

    <TextView

        android:id="@+id/tv_b"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="b"

        app:layout_constraintStart_toEndOf="@id/tv_a"

        app:layout_constraintEnd_toStartOf="@+id/tv_c" />

    <TextView

        android:id="@+id/tv_c"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="c"

        app:layout_constraintStart_toEndOf="@id/tv_b"

        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>


如果不设置chainStyle默认展示样式就是Spread Chain

五、constraintDimensionRatio 宽高比

<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">

    <ImageView

        android:layout_width="123dp"

        android:layout_height="0dp"

        android:src="@color/pb_blue"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintDimensionRatio="5:3"

        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


六、constraintBaseline_toBaselineOf  中线对齐

<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:id="@+id/baseline_1"

        android:layout_width="wrap_content"

        android:layout_height="50dp"

        app:layout_constraintVertical_bias="0.3"

        app:layout_constraintHorizontal_bias="0.2"

        android:background="@color/pb_blue"

        android:gravity="center"

        android:text="baseline1"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <TextView

        android:id="@+id/baseline_2"

        android:layout_width="0dp"

        android:layout_height="100dp"

        android:layout_marginLeft="20dp"

        android:background="@color/pb_blue"

        app:layout_constraintDimensionRatio="1:1"

        android:gravity="center"

        android:text="baseline2"

        app:layout_constraintBaseline_toBaselineOf="@id/baseline_1"

        app:layout_constraintStart_toEndOf="@id/baseline_1" />

</android.support.constraint.ConstraintLayout>


七、android.support.constraint.Group 把相关控件放到一个组里统一控制隐藏或显示

<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">

    <ImageView

        android:id="@+id/profile_image"

        android:layout_width="123dp"

        android:layout_height="0dp"

        android:src="@color/pb_blue"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintDimensionRatio="5:3"

        app:layout_constraintTop_toTopOf="parent" />

    <TextView

        android:id="@+id/profile_name"

        android:layout_width="123dp"

        android:layout_height="0dp"

        android:text="hello"

        app:layout_constraintDimensionRatio="5:3"

        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Group

        android:id="@+id/profile"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:visibility="gone"

        app:constraint_referenced_ids="profile_name,profile_image" />

</android.support.constraint.ConstraintLayout>


八、layout_constrainWidth_percent layout_constrainHeight_percent

这两个属性的作用就是指定当前控件的宽度或高度是父控件的百分之多少。可设置的值在 0 - 1 之间,1 就是 100%。

<ImageView

        android:layout_width="0dp"

        android:layout_height="0dp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintWidth_percent="0.8"

        app:layout_constraintDimensionRatio="5:3"

        android:src="@color/pb_blue"

        app:layout_constraintTop_toTopOf="parent" />


九、android.support.constraint.Placeholder

Placeholder(占位符)是一个虚拟对象,作用和它的名字一样,就是占位。

当放置好 Placeholder 后,可以通过 setContentId() 方法将占位符变为有效的视图。如果视图已经存在于屏幕上,那么视图将会从原有位置消失。

除此之外,还可以通过 setEmptyVisibility() 方法设置当视图不存在时占位符的可见性。

下面的例子演示了占位符的使用,当点击顶部头像时,顶部头像会消失并在占位符处显示:

<?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"

    android:padding="16dp"

    tools:context="com.github.airsaid.constraintlayoutdemo.MainActivity">

    <ImageView

        android:id="@+id/avatar"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@mipmap/ic_avatar"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Placeholder

        android:id="@+id/placeholder"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

avatar.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                TransitionManager.beginDelayedTransition(constraintLayout);

                placeholder.setContentId(view.getId());

            }

        });

十、android.support.constraint.Barrier

当我们在布局时,有时候就会遇到布局会随着数据的多少而改变大小的情况。以下图为例:


以前的做法是把A,B外包一个Layout,然后C设置在Layout的右侧

通过上图就可以发现,当在 A、B 控件的大小都不确定的情况下,C 以谁作为约束对象都不对。如果以 A 作为约束对象,那么当 B 的宽度过宽时就会被遮挡,同理以 B 作为约束也是如此。

那么此时,Barrier(屏障)就派上用场了。这是个非常好用的东东,和 GuideLine 一样,它是一个虚拟的 View,对界面是不可见的。目的就是辅助布局。

对 Barrier 可以使用的属性有:

barrierDirection:设置 Barrier  所创建的位置。可设置的有:bottom、end、left、right、start、top。

constraint_referenced_ids:设置 Barrier 引用的控件。可设置多个,设置的方式是:id, id。(无需加 @id/)

barrierAllowsGoneWidgets:默认为 true,即当 Barrier 引用的控件被 GONE 掉时,则 Barrier 默认的创建行为是在已 GONE 掉控件的已解析位置上进行创建。如果设置为 false,则不会将 GONE 掉的控件考虑在内。

说再多不如看代码,还是以上图为例,来看看 Barrier 是如何解决的:

<?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"

    android:padding="16dp">

    <TextView

        android:id="@+id/title"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Title"

        android:textSize="16sp"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <TextView

        android:id="@+id/desc"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:text="This is a descriptive text."

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@id/title" />

<android.support.constraint.Barrier

        android:id="@+id/barrier"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        app:barrierDirection="end"

        app:constraint_referenced_ids="title, desc" />

    <TextView

        android:id="@+id/content"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_marginLeft="10dp"

        android:text="This is a piece of content that is very long and long very long and long ..."

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toEndOf="@id/barrier"

        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


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

推荐阅读更多精彩内容