ConstraintLayout使用实战
实战演示效果
性能优势:
Android系统在绘制控件的时候,会经过测量,布局,绘制三个过程,每个过程都会自顶向下遍历视图树。所以布局嵌套层次越深,设备绘制视图所需的时间和计算功耗也就越多。 ConstraintLayout 容器就是用来构建复杂布局的,而不必嵌套 View 和 ViewGroup 元素,实现完全扁平的层次结构。
自顶向下绘制过程图使用介绍:
-
相对定位
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
效果类似于RelativeLayout的相对定位使用效果,以上属性需要另一个控件的id或parent作为参考。
-
Guideline
Guideline是只能用在ConstraintLayout布局里面的一个工具类,用于辅助布局,类似为辅助线,主要作用是将该页面的控件统一设置左侧对齐或右侧对齐。Guideline是不会显示到界面上的,默认是GONE的。
layout_constraintGuide_begin
layout_constraintGuide_end
layout_constraintGuide_percent
-
居中定位
layout_constraintLeft_toLeftOf
layout_constraintRight_toRightOf
同时设置同一控件id即可与该控件居中对齐。
-
偏压
layout_constraintHorizontal_bias
layout_constraintVertical_bias
在居中对齐的基础上,设置居于垂直或水品位置的比例。
-
圆形定位
layout_constraintCircle :依赖哪个控件进行布局
layout_constraintCircleRadius :到依赖对象中心的距离
layout_constraintCircleAngle :当前要摆放的控件应处于哪个角度
-
尺寸限制
android:minWidth 设置布局的最小宽度
android:minHeight 设置布局的最小高度
android:maxWidth 设置布局的最大宽度
android:maxHeight 设置布局的最大高度
要设置尺寸限制,需要在宽高都设为wrap_content才能生效。
-
Ratio比例
layout_constraintDimensionRatio
可设定该控件通过宽高比或者高宽比来限定控件的比例。
-
Chains链
设置一条线上布局的对齐属性。
-
Group
Group 用于控制所引用的一组控件的可见性。用如下属性来设置多个控件id,通过对自己的可见性改变统一改变这些控件的可见性。
constraint_referenced_ids
-
Placeholder
通过设置app:content=“id”,将id View的内容绘制到自己的位置上,而原id的 View就像gone了一样。
布局文件代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MineActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="15dp"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="27dp"/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="15dp"/>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/orange2"
app:layout_constraintDimensionRatio="100:35"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv_scan"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/scan"
android:padding="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/iv_edit"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/edit"
android:padding="14dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/login_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="12dp"
android:paddingBottom="20dp"
android:elevation="2dp"
android:background="@drawable/shape_round_white"
app:layout_constraintTop_toBottomOf="@id/iv_edit"
android:layout_margin="15dp">
<ImageView
android:id="@+id/iv_head"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="13dp"
android:background="@mipmap/default_head"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/tv_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击登录"
android:textSize="18sp"
android:textColor="@color/black"
app:layout_constraintLeft_toRightOf="@id/iv_head"
app:layout_constraintTop_toTopOf="@id/iv_head"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="成就你的写作梦想"
android:maxWidth="200dp"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@id/iv_head"
app:layout_constraintLeft_toLeftOf="@id/tv_login"
android:layout_marginBottom="5dp"/>
<ImageView
android:id="@+id/iv_gift"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@mipmap/gift"
app:layout_constraintVertical_bias="0.12"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="免费领"
android:textSize="8sp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="1dp"
android:paddingBottom="1dp"
app:layout_constraintLeft_toLeftOf="@id/iv_gift"
app:layout_constraintRight_toRightOf="@id/iv_gift"
app:layout_constraintTop_toBottomOf="@id/iv_gift"
android:background="@drawable/shape_roung_perple"
android:textColor="@color/white"/>
<View
android:id="@+id/view_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/light_gray"
app:layout_constraintTop_toBottomOf="@id/iv_head"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<ImageView
android:id="@+id/iv_ic1"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/pic1"
android:layout_marginTop="14dp"
android:layout_marginLeft="20dp"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/iv_ic2"
app:layout_constraintTop_toTopOf="@id/view_line"/>
<TextView
android:id="@+id/tv_my_artical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的文章"
android:textColor="@color/black"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@id/iv_ic1"
app:layout_constraintLeft_toLeftOf="@id/iv_ic1"
app:layout_constraintRight_toRightOf="@id/iv_ic1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0篇私密"
android:layout_marginTop="5dp"
android:textSize="11sp"
app:layout_constraintTop_toBottomOf="@id/tv_my_artical"
app:layout_constraintLeft_toLeftOf="@id/iv_ic1"
app:layout_constraintRight_toRightOf="@id/iv_ic1"/>
<ImageView
android:id="@+id/iv_ic2"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/pic2"
android:layout_marginTop="14dp"
app:layout_constraintLeft_toRightOf="@id/iv_ic1"
app:layout_constraintRight_toLeftOf="@id/iv_ic3"
app:layout_constraintTop_toTopOf="@id/view_line"/>
<TextView
android:id="@+id/tv_my_sticker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的帖子"
android:layout_marginTop="12dp"
android:textColor="@color/black"
app:layout_constraintTop_toBottomOf="@id/iv_ic2"
app:layout_constraintLeft_toLeftOf="@id/iv_ic2"
app:layout_constraintRight_toRightOf="@id/iv_ic2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0个帖子"
android:layout_marginTop="5dp"
android:textSize="11sp"
app:layout_constraintTop_toBottomOf="@id/tv_my_sticker"
app:layout_constraintLeft_toLeftOf="@id/iv_ic2"
app:layout_constraintRight_toRightOf="@id/iv_ic2"/>
<ImageView
android:id="@+id/iv_ic3"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/pic3"
android:layout_marginTop="14dp"
app:layout_constraintLeft_toRightOf="@id/iv_ic2"
app:layout_constraintRight_toLeftOf="@id/iv_ic4"
app:layout_constraintTop_toTopOf="@id/view_line"/>
<TextView
android:id="@+id/tv_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="赞和收藏"
android:layout_marginTop="12dp"
android:textColor="@color/black"
app:layout_constraintTop_toBottomOf="@id/iv_ic3"
app:layout_constraintLeft_toLeftOf="@id/iv_ic3"
app:layout_constraintRight_toRightOf="@id/iv_ic3"/>
<ImageView
android:id="@+id/iv_ic4"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/pic4"
android:layout_marginTop="14dp"
android:layout_marginRight="20dp"
app:layout_constraintLeft_toRightOf="@id/iv_ic3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/view_line"/>
<TextView
android:id="@+id/tv_my_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="赞和收藏"
android:layout_marginTop="12dp"
android:textColor="@color/black"
app:layout_constraintTop_toBottomOf="@id/iv_ic4"
app:layout_constraintLeft_toLeftOf="@id/iv_ic4"
app:layout_constraintRight_toRightOf="@id/iv_ic4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="含已购内容"
android:layout_marginTop="5dp"
android:textSize="11sp"
app:layout_constraintTop_toBottomOf="@id/tv_my_book"
app:layout_constraintLeft_toLeftOf="@id/iv_ic4"
app:layout_constraintRight_toRightOf="@id/iv_ic4"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_round_white"
app:layout_constraintTop_toBottomOf="@id/login_box"
android:layout_margin="15dp"
android:elevation="2dp"
android:padding="12dp">
<androidx.constraintlayout.widget.Group
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="tv_assets,tv_check,view_line2"/>
<TextView
android:id="@+id/tv_assets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="总资产:0"
android:textColor="@color/black"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/tv_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看"
android:textSize="12sp"
android:gravity="center_vertical"
android:drawableRight="@mipmap/arrow_right"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:drawablePadding="8dp"/>
<View
android:id="@+id/view_line2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/light_gray"
app:layout_constraintTop_toBottomOf="@id/tv_assets"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<ImageView
android:id="@+id/iv_dimends"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/dimends"
app:layout_constraintTop_toTopOf="@id/view_line2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/iv_vip"
android:layout_marginLeft="15dp"
app:layout_constraintHorizontal_chainStyle="spread_inside"
android:layout_marginTop="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="简书钻"
app:layout_constraintLeft_toLeftOf="@id/iv_dimends"
app:layout_constraintRight_toRightOf="@id/iv_dimends"
app:layout_constraintTop_toBottomOf="@id/iv_dimends"
android:layout_marginTop="8dp"/>
<ImageView
android:id="@+id/iv_vip"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/vip"
app:layout_constraintTop_toTopOf="@id/view_line2"
app:layout_constraintLeft_toRightOf="@id/iv_dimends"
app:layout_constraintRight_toLeftOf="@id/iv_prize"
android:layout_marginTop="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="简书会员"
app:layout_constraintLeft_toLeftOf="@id/iv_vip"
app:layout_constraintRight_toRightOf="@id/iv_vip"
app:layout_constraintTop_toBottomOf="@id/iv_vip"
android:layout_marginTop="8dp"/>
<ImageView
android:id="@+id/iv_prize"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/prize"
app:layout_constraintTop_toTopOf="@id/view_line2"
app:layout_constraintLeft_toRightOf="@id/iv_vip"
app:layout_constraintRight_toLeftOf="@id/iv_rank"
android:layout_marginTop="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="天天抽奖"
app:layout_constraintLeft_toLeftOf="@id/iv_prize"
app:layout_constraintRight_toRightOf="@id/iv_prize"
app:layout_constraintTop_toBottomOf="@id/iv_prize"
android:layout_marginTop="8dp"/>
<ImageView
android:id="@+id/iv_rank"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/rank"
app:layout_constraintTop_toTopOf="@id/view_line2"
app:layout_constraintLeft_toRightOf="@id/iv_prize"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="排行榜"
app:layout_constraintLeft_toLeftOf="@id/iv_rank"
app:layout_constraintRight_toRightOf="@id/iv_rank"
app:layout_constraintTop_toBottomOf="@id/iv_rank"
android:layout_marginTop="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:id="@+id/view_topline"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/menu"
android:layout_marginTop="10dp"/>
<View
android:id="@+id/view_line3"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/view_topline"
android:layout_marginTop="48dp"/>
<View
android:id="@+id/view_line4"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/view_line3"
android:layout_marginTop="48dp"/>
<View
android:id="@+id/view_line5"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/view_line4"
android:layout_marginTop="48dp"/>
<View
android:id="@+id/view_line6"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/view_line5"
android:layout_marginTop="48dp"/>
<View
android:id="@+id/view_line7"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/view_line6"
android:layout_marginTop="48dp"/>
<View
android:id="@+id/view_line8"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/view_line7"
android:layout_marginTop="48dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的钱包"
android:textSize="15sp"
android:textColor="@color/black"
app:layout_constraintTop_toTopOf="@id/view_topline"
app:layout_constraintBottom_toBottomOf="@id/view_line3"
app:layout_constraintLeft_toLeftOf="@id/guideline_left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的优惠券"
app:layout_constraintTop_toTopOf="@id/view_topline"
app:layout_constraintBottom_toBottomOf="@id/view_line3"
app:layout_constraintRight_toRightOf="@id/guideline_right"
android:drawableRight="@mipmap/arrow_right"
android:drawablePadding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="简书活动"
android:textSize="15sp"
android:textColor="@color/black"
app:layout_constraintTop_toTopOf="@id/view_line3"
app:layout_constraintBottom_toBottomOf="@id/view_line4"
app:layout_constraintLeft_toLeftOf="@id/guideline_left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/view_line3"
app:layout_constraintBottom_toBottomOf="@id/view_line4"
app:layout_constraintRight_toRightOf="@id/guideline_right"
android:drawableRight="@mipmap/arrow_right"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的小岛/专题/文集"
android:textSize="15sp"
android:textColor="@color/black"
app:layout_constraintTop_toTopOf="@id/view_line4"
app:layout_constraintBottom_toBottomOf="@id/view_line5"
app:layout_constraintLeft_toLeftOf="@id/guideline_left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/view_line4"
app:layout_constraintBottom_toBottomOf="@id/view_line5"
app:layout_constraintRight_toRightOf="@id/guideline_right"
android:drawableRight="@mipmap/arrow_right"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="浏览历史"
android:textSize="15sp"
android:textColor="@color/black"
app:layout_constraintTop_toTopOf="@id/view_line5"
app:layout_constraintBottom_toBottomOf="@id/view_line6"
app:layout_constraintLeft_toLeftOf="@id/guideline_left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/view_line5"
app:layout_constraintBottom_toBottomOf="@id/view_line6"
app:layout_constraintRight_toRightOf="@id/guideline_right"
android:drawableRight="@mipmap/arrow_right"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="意见反馈"
android:textSize="15sp"
android:textColor="@color/black"
app:layout_constraintTop_toTopOf="@id/view_line6"
app:layout_constraintBottom_toBottomOf="@id/view_line7"
app:layout_constraintLeft_toLeftOf="@id/guideline_left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/view_line6"
app:layout_constraintBottom_toBottomOf="@id/view_line7"
app:layout_constraintRight_toRightOf="@id/guideline_right"
android:drawableRight="@mipmap/arrow_right"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置"
android:textSize="15sp"
android:textColor="@color/black"
app:layout_constraintTop_toTopOf="@id/view_line7"
app:layout_constraintBottom_toBottomOf="@id/view_line8"
app:layout_constraintLeft_toLeftOf="@id/guideline_left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/view_line7"
app:layout_constraintBottom_toBottomOf="@id/view_line8"
app:layout_constraintRight_toRightOf="@id/guideline_right"
android:drawableRight="@mipmap/arrow_right"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>