ContraintLayout使用

1、效果图如下:

Screenshot_20170402-181602.png

2、布局最多嵌套三层,复杂布局再也不用担心嵌套层次更多啦

<?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:background="@android:color/white">

    <TextView
        android:id="@+id/header_tv"
        android:layout_width="0dp"
        android:layout_height="44dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="旅游介绍"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:elevation="8dp"
        android:translationZ="8dp"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header_tv">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.constraint.Guideline
                android:id="@+id/guide_line_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.333" />

            <android.support.constraint.Guideline
                android:id="@+id/guide_line_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.666" />

            <ImageView
                android:id="@+id/beach_iv"
                android:layout_width="44dp"
                android:layout_height="44dp"
                android:layout_marginTop="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/guide_line_left"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_beach"
                app:layout_constraintHorizontal_bias="0.452" />

            <ImageView
                android:id="@+id/desert_iv"
                android:layout_width="44dp"
                android:layout_height="44dp"
                android:layout_marginTop="24dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_desert" />

            <ImageView
                android:id="@+id/forest_iv"
                android:layout_width="44dp"
                android:layout_height="44dp"
                android:layout_marginTop="24dp"
                app:layout_constraintLeft_toLeftOf="@id/guide_line_right"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_forest" />

            <TextView
                android:id="@+id/desert_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="沙漠"
                android:textSize="13sp"
                app:layout_constraintLeft_toLeftOf="@id/guide_line_left"
                app:layout_constraintRight_toLeftOf="@id/guide_line_right"
                app:layout_constraintTop_toBottomOf="@id/desert_iv" />

            <TextView
                android:id="@+id/beach_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="沙滩"
                android:textSize="13sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/guide_line_left"
                app:layout_constraintTop_toTopOf="@id/desert_tv" />

            <TextView
                android:id="@+id/forest_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="森林"
                android:textSize="13sp"
                app:layout_constraintLeft_toLeftOf="@id/guide_line_right"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="@id/desert_tv" />


            <!--***************************************************************************************-->

            <ImageView
                android:id="@+id/island_iv"
                android:layout_width="44dp"
                android:layout_height="44dp"
                android:layout_marginTop="24dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/guide_line_left"
                app:layout_constraintTop_toBottomOf="@id/desert_tv"
                app:srcCompat="@drawable/ic_island" />

            <ImageView
                android:id="@+id/mountain_iv"
                android:layout_width="44dp"
                android:layout_height="44dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="@id/island_iv"
                app:srcCompat="@drawable/ic_mountain" />

            <ImageView
                android:id="@+id/waterfall_iv"
                android:layout_width="44dp"
                android:layout_height="44dp"
                app:layout_constraintLeft_toLeftOf="@+id/guide_line_right"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="@id/island_iv"
                app:srcCompat="@drawable/ic_waterfall" />

            <TextView
                android:id="@+id/mountain_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="高山"
                android:textSize="13sp"
                app:layout_constraintLeft_toLeftOf="@id/guide_line_left"
                app:layout_constraintRight_toLeftOf="@id/guide_line_right"
                app:layout_constraintTop_toBottomOf="@id/mountain_iv" />

            <TextView
                android:id="@+id/island_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="海岛"
                android:textSize="13sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/guide_line_left"
                app:layout_constraintTop_toTopOf="@id/mountain_tv" />

            <TextView
                android:id="@+id/waterfall_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="瀑布"
                android:textSize="13sp"
                app:layout_constraintLeft_toLeftOf="@id/guide_line_right"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="@id/mountain_tv" />

            <!--***************************************************************************************-->
            <TextView
                android:id="@+id/hot_tv"
                android:layout_width="0dp"
                android:layout_height="44dp"
                android:layout_marginTop="16dp"
                android:background="#fff6f7f9"
                android:gravity="center_vertical"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:text="热门游记"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/mountain_tv" />

            <TextView
                android:id="@+id/whole_tv"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:gravity="center_vertical"
                android:text="查看全部"
                android:tint="@android:color/white"
                android:textColor="#FF5296F0"
                app:layout_constraintBottom_toBottomOf="@id/hot_tv"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="@id/hot_tv" />

            <!--***************************************************************************************-->

            <ImageView
                android:id="@+id/arrow_iv"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="8dp"
                android:padding="8dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/whole_tv"
                app:srcCompat="@drawable/icon_arrow" />

            <TextView
                android:id="@+id/title_tv"
                android:layout_width="0dp"
                android:layout_height="32dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:gravity="center_vertical"
                android:text="1. 书写着火山奇迹的理想之地"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/arrow_iv"
                app:layout_constraintTop_toBottomOf="@id/hot_tv" />

            <TextView
                android:id="@+id/subtitle_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:gravity="center_vertical"
                android:lineSpacingMultiplier="1.2"
                android:text="又到2月14日情人节,要说最不激动,最没诗意、最不浪漫的,也是最为淳朴、最为踏实的,就是我们这种“老夫老妻”,尽管这个浪漫的节日已经超级商业化,我们本身也这么认为,但心依旧。"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/title_tv" />

            <!--***************************************************************************************-->

            <ImageView
                android:id="@+id/arrow_2_iv"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="8dp"
                android:padding="8dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/subtitle_tv"
                app:srcCompat="@drawable/icon_arrow" />

            <TextView
                android:id="@+id/title_2_tv"
                android:layout_width="0dp"
                android:layout_height="32dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:gravity="center_vertical"
                android:text="2. 情系大西北游记"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/arrow_2_iv"
                app:layout_constraintTop_toBottomOf="@id/subtitle_tv" />

            <TextView
                android:id="@+id/subtitle_2_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:gravity="center_vertical"
                android:lineSpacingMultiplier="1.2"
                android:text="一整晚没睡,凌晨3点早早就蹦去 深圳 机场…还是一如既往、开错了路,挨着骂终于赶上了飞机! 出发出发出发!大 西北 ,我们来咯、准备开启8天的三人行哟! "
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/title_2_tv" />

            <!--***************************************************************************************-->

            <ImageView
                android:id="@+id/arrow_3_iv"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="8dp"
                android:padding="8dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/subtitle_2_tv"
                app:srcCompat="@drawable/icon_arrow" />

            <TextView
                android:id="@+id/title_3_tv"
                android:layout_width="0dp"
                android:layout_height="32dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:gravity="center_vertical"
                android:text="3. 只是想出去走一走——7天6西安游~"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/arrow_3_iv"
                app:layout_constraintTop_toBottomOf="@id/subtitle_2_tv" />

            <TextView
                android:id="@+id/subtitle_3_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:gravity="center_vertical"
                android:lineSpacingMultiplier="1.2"
                android:text="写在前面的话:旅行是一件会上瘾的事情。只要开始了,就停不下来。"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/title_3_tv" />

            <!--***************************************************************************************-->

            <ImageView
                android:id="@+id/arrow_4_iv"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="8dp"
                android:padding="8dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/subtitle_3_tv"
                app:srcCompat="@drawable/icon_arrow" />

            <TextView
                android:id="@+id/title_4_tv"
                android:layout_width="0dp"
                android:layout_height="32dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:gravity="center_vertical"
                android:text="4. 我画中的八月黄姚-美丽桂林"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/arrow_4_iv"
                app:layout_constraintTop_toBottomOf="@id/subtitle_3_tv" />

            <TextView
                android:id="@+id/subtitle_4_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:gravity="center_vertical"
                android:lineSpacingMultiplier="1.2"
                android:text="这篇游记开始写文字部分的时候已经是十月份了,不过似乎这十天的行程陪伴到了我十月。大家肯定也会问我一个问题,这些画是你在旅途中画的吗?答案:不是!"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginBottom="16dp"
                app:layout_constraintTop_toBottomOf="@id/title_4_tv" />
        </android.support.constraint.ConstraintLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,263评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,979评论 4 60
  • 昨夜刮了一夜的风,幽幽怨怨、哼哼唧唧的,下午便收到手机资讯,意思是21日气温突变,最低温...
    清辉V阅读 254评论 0 1
  • 夜,不知道雨何时会停 落在了发丝,脸颊还有手心 习惯的在下着雨的夜里独自行走 看着远远的灯光,伴着脚步声,前行 我...
    我走以后阅读 205评论 0 0
  • 曾经多少次 梦过巫山 无暇,也只是梦中 游过 一览神女 应无恙 今天,似是如约而至 我迫不及待地 登上甲板 倚栏而...
    那年五月阅读 564评论 0 7