布局性能優(yōu)化之ConstraintLayout布局

布局優(yōu)化--減少布局嵌套層級(jí)

當(dāng)布局嵌套深入比較深的時(shí)候,往往會(huì)伴隨著一些性能問題。所以很多時(shí)候我們建議使用RelativeLayout或者GridLayout來簡化掉布局的深度。

而對(duì)于簡化布局深度,ConstraintLayout幾乎可以做到極致,接下來我們通過實(shí)例來盡可能將所有常見的屬性一步步的介紹清楚。

constriantLayout布局引入

首先需要引入我們的ConstraintLayout,在build.gradle中加入:

compile'com.android.support.constraint:constraint-layout:1.0.2'

1,app:layout_constraintLeft_toLeftOf="parent"

? ? ? app:layout_constraintLeft_toLeftOf="@id/viewB"

? ? ? layout_constraintLeft_toLeftOf表示和什么左邊對(duì)齊

2,app:layout_constraintLeft_toRightOf="@id/viewB"

? ? ? layout_constraintLeft_toRightOf表示當(dāng)前控件的左側(cè)在那個(gè)控件的右側(cè)

3,與之類似的還有幾個(gè)屬性:

? ? ? layout_constraintRight_toLeftOf

? ? ? layout_constraintRight_toRightOf?

? ? ? layout_constraintTop_toTopOfth

? ? ? layout_constraintTop_toBottomOf

? ? ? layout_constraintBottom_toTopOf

? ? ? layout_constraintBottom_toBottomOf

? ? ? layout_constraintBaseline_toBaselineOf

4,與RelativeLayout的區(qū)別:

? ? ? 當(dāng)一個(gè)控件的兩端都加入的約束的時(shí)候,需要設(shè)置layout_width = 0;

? ? ? 在COnstraintLayout布局中是不存在match_parent屬性的,

5,設(shè)置寬高比

? ? ? android:layout_width="0dp"

? ? ? android:layout_height="0dp"

? ? ? app:layout_constraintLeft_toLeftOf="parent"

? ? ? app:layout_constraintRight_toRightOf="parent"

? ? ? app:layout_constraintDimensionRatio="16:6"

? ? ? 這個(gè)寬高比屬性,還支持這樣的寫法:

? ? ? app:layout_constraintDimensionRatio="W,16:6"

? ? ? app:layout_constraintDimensionRatio="H,16:6"

6,增加幾個(gè)Tab

? ? ? 現(xiàn)在我們希望在底部增加3個(gè)tab,均分。是不是想到了LinearLayout和weight。

? ? ? 使用ConstraintLayout實(shí)現(xiàn)如下

? ? ? <TextView?

? ? ? ? ?android:id="@+id/tab1"

? ? ? ? ?android:layout_width="0dp"

? ? ? ? ?android:layout_height="30dp"

? ? ? ? ?app:layout_constraintBottom_toBottomOf="parent"

? ? ? ? ?app:layout_constraintLeft_toLeftOf="parent"

? ? ? ? ?app:layout_constraintRight_toLeftOf="@+id/tab2"/>?

? ? ? <TextView

? ? ? ? ?android:id="@+id/tab2"

? ? ? ? ?android:layout_width="0dp"

? ? ? ? ?android:layout_height="30dp"

? ? ? ? ?app:layout_constraintBottom_toBottomOf="parent"

? ? ? ? ?app:layout_constraintLeft_toRightOf="@id/tab1"

? ? ? ? ?app:layout_constraintRight_toLeftOf="@+id/tab3"/>?

? ? ? <TextView

? ? ? ? ?android:id="@+id/tab3"

? ? ? ? ?android:layout_width="0dp"

? ? ? ? ?android:layout_height="30dp"

? ? ? ? ?app:layout_constraintBottom_toBottomOf="parent"

? ? ? ? ?app:layout_constraintLeft_toRightOf="@id/tab2"

? ? ? ? ?app:layout_constraintRight_toRightOf="parent"/>

? ? ? ? 我們看橫向的依賴,3個(gè)tab兩兩設(shè)置了約束(即你在我們的左邊,我在你的右邊),最外層的設(shè)置了parent約束;再加上我們把寬度都設(shè)置為??

? ? ? ? 了match_constraint,so

1.png

app:layout_constraintHorizontal_weight比重屬性設(shè)置:

假設(shè)我們分別設(shè)置值為2,1,1。

2.png

layout_constraintHorizontal_chainStyle設(shè)置展示效果:

chainStyle=”spread”+ 寬度為0:

3.png

chainStyle=”spread”??+ 寬度非0:

4.png

chainStyle=”spread_inside”?+ 寬度非0

5.png

chainStyle=”packed”+ 寬度非0

6.png

7,一個(gè)很常見的功能,我們現(xiàn)在希望在右下角增加一個(gè)浮動(dòng)按鈕。

? ? ?<TextView

? ? ? ? android:layout_width="60dp"

? ? ? ? android:layout_height="60dp"

? ? ? ? android:background="#612"

? ? ? ? app:layout_constraintVertical_bias="0.9"

? ? ? ? app:layout_constraintHorizontal_bias="0.9"

? ? ? ? app:layout_constraintBottom_toBottomOf="parent"

? ? ? ? app:layout_constraintLeft_toLeftOf="parent"

? ? ? ? app:layout_constraintRight_toRightOf="parent"

? ? ? ? app:layout_constraintTop_toTopOf="parent"/>?

7.png

? ? ? ? ?layout_constraintHorizontal_bias

? ? ? ? ?即設(shè)置上下兩側(cè)間隙比例分別為90%與10%,設(shè)置左右兩側(cè)間隙比例分別為90%與10%。

? ? ? ? ?android.support.constraint.Guideline 該類比較簡單,主要用于輔助布局,即類似為輔助線,橫向的、縱向的。該布局是不會(huì)顯示到界面上的。

? ? ? ? ?android:orientation取值為”vertical”和”horizontal”.

? ? ? ? ?除此以外,還差個(gè)屬性,決定該輔助線的位置:

? ? ? ? ?layout_constraintGuide_begin

? ? ? ? ?layout_constraintGuide_end

? ? ? ? ?layout_constraintGuide_percent

? ? ? ? ?可以通過上面3個(gè)屬性其中之一來確定屬性值位置。

? ? ? ? ?begin=30dp,即可認(rèn)為距離頂部30dp的地方有個(gè)輔助線,根據(jù)orientation來決定是橫向還是縱向。

? ? ? ? ?end=30dp,即為距離底部。?

? ? ? ? ?percent=0.8即為距離頂部80%。

? ?<android.support.constraint.ConstraintLayout

? ? ? ? ...

? ? ? ? tools:context="com.zhy.constrantlayout_learn.MainActivity">

????<android.support.constraint.Guideline

????????android:id="@+id/guideline_h"

????????android:layout_width="wrap_content"

????????android:layout_height="wrap_content"

????????android:orientation="horizontal"

????????app:layout_constraintGuide_percent="0.8" />

????<android.support.constraint.Guideline

????????android:id="@+id/guideline_w"

????????android:layout_width="wrap_content"

????????android:layout_height="wrap_content"

????????android:orientation="vertical"

????????app:layout_constraintGuide_percent="0.8" />

????<TextView

????????android:layout_width="60dp"

????????android:layout_height="60dp"

????????android:background="#612"

????????app:layout_constraintLeft_toRightOf="@id/guideline_w"

????????app:layout_constraintTop_toBottomOf="@id/guideline_h" />

</....>

8.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容