實(shí)戰(zhàn)演示效果
性能優(yōu)勢(shì):
Android系統(tǒng)在繪制控件的時(shí)候,會(huì)經(jīng)過測(cè)量鹤啡,布局肌稻,繪制三個(gè)過程清蚀,每個(gè)過程都會(huì)自頂向下遍歷視圖樹。所以布局嵌套層次越深爹谭,設(shè)備繪制視圖所需的時(shí)間和計(jì)算功耗也就越多枷邪。 ConstraintLayout 容器就是用來構(gòu)建復(fù)雜布局的,而不必嵌套 View 和 ViewGroup 元素诺凡,實(shí)現(xiàn)完全扁平的層次結(jié)構(gòu)东揣。
使用介紹:
-
相對(duì)定位
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的相對(duì)定位使用效果,以上屬性需要另一個(gè)控件的id或parent作為參考腹泌。
其中嘶卧,很多布局控件都有 start 及 end 相關(guān)的參數(shù),使用起來好像跟 left 和 right 沒什么區(qū)別凉袱,實(shí)際上芥吟,控件方向一般來說都是從左到右,但也有部分中東國(guó)家的習(xí)慣是從右到左专甩,當(dāng)控件方向是從左到右時(shí)钟鸵,start 等于 left,end 等于 right涤躲;控件方向從右至左時(shí)棺耍,start 等于 right,end 等于 left种樱,用start烈掠、end就兼容了左右方向了。
-
Guideline
Guideline是只能用在ConstraintLayout布局里面的一個(gè)工具類缸托,用于輔助布局左敌,類似為輔助線,主要作用是將該頁面的控件統(tǒng)一設(shè)置左側(cè)對(duì)齊或右側(cè)對(duì)齊俐镐。Guideline是不會(huì)顯示到界面上的矫限,默認(rèn)是GONE的。
layout_constraintGuide_begin
layout_constraintGuide_end
layout_constraintGuide_percent
-
居中定位
layout_constraintLeft_toLeftOf
layout_constraintRight_toRightOf
同時(shí)設(shè)置同一控件id即可與該控件居中對(duì)齊佩抹。
-
偏壓
layout_constraintHorizontal_bias
layout_constraintVertical_bias
在居中對(duì)齊的基礎(chǔ)上叼风,設(shè)置居于垂直或水品位置的比例。
-
圓形定位
layout_constraintCircle :依賴哪個(gè)控件進(jìn)行布局
layout_constraintCircleRadius :到依賴對(duì)象中心的距離
layout_constraintCircleAngle :當(dāng)前要擺放的控件應(yīng)處于哪個(gè)角度
-
尺寸限制
android:minWidth 設(shè)置布局的最小寬度
android:minHeight 設(shè)置布局的最小高度
android:maxWidth 設(shè)置布局的最大寬度
android:maxHeight 設(shè)置布局的最大高度
要設(shè)置尺寸限制棍苹,需要在寬高都設(shè)為wrap_content才能生效无宿。
-
Ratio比例
layout_constraintDimensionRatio
可設(shè)定該控件通過寬高比或者高寬比來限定控件的比例。
-
Chains鏈
設(shè)置一條線上布局的對(duì)齊屬性枢里。
-
Group
Group 用于控制所引用的一組控件的可見性孽鸡。用如下屬性來設(shè)置多個(gè)控件id蹂午,通過對(duì)自己的可見性改變統(tǒng)一改變這些控件的可見性。
constraint_referenced_ids
-
Placeholder
通過設(shè)置app:content=“id”彬碱,將id View的內(nèi)容繪制到自己的位置上豆胸,而原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="點(diǎn)擊登錄"
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="成就你的寫作夢(mèng)想"
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="免費(fèi)領(lǐng)"
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個(gè)帖子"
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="含已購內(nèi)容"
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="總資產(chǎn):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="簡(jiǎn)書鉆"
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="簡(jiǎn)書會(huì)員"
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="天天抽獎(jiǎng)"
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="我的優(yōu)惠券"
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="簡(jiǎn)書活動(dòng)"
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="設(shè)置"
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>