layout布局_Android ConstraintLayout 降低布局层次,布局优化首选
目錄
1. 介紹
2. ConstraintLayout的優(yōu)點(diǎn)
3. ConstraintLayout使用方法
3.1 Relative positioning(相對(duì)定位)
3.2 Margins(邊距)
3.3 Centering positioning(居中偏移)
3.4 Circular positioning(圓心偏移)
3.5 Visibility behavior(隱藏行為)
3.6 Dimension constraints(尺寸約束)
3.7 Chains(鏈)
4. Virtual Helpers objects(輔助工具)
4.1 Optimizer(計(jì)算優(yōu)化)
4.2 Barrier(屏障)
4.3 Group(分組)
4.4 PlaceHolder(占位符)
4.5 GuideLine(基準(zhǔn)線)
1.介紹
Contrstraint Layout Library包是Android support library的一部分,現(xiàn)在不再維護(hù)了。Contrstraint Layout Library已經(jīng)被AndroidX約束布局庫(kù)所取代,后者是Jetpack的一部分。
ConstraintLayout類(lèi)已經(jīng)被androidx.constraintlayout.widget.ConstraintLayout所取代。我們建議在所有新項(xiàng)目中使用AndroidX庫(kù)。
您還應(yīng)該考慮將現(xiàn)有項(xiàng)目遷移到AndroidX。
2 .ConstrainLayou優(yōu)點(diǎn)
1. 減少布局的層次,減少過(guò)度繪制。
復(fù)雜的布局一般會(huì)用RelativeLayout嵌套多層RelativeLayout,這樣會(huì)導(dǎo)致布局層次
過(guò)深,性能會(huì)很低
2. 適配上更靈活
ConstrainLayout可以按照比例約束控制尺寸和位置能夠更好的適配不通的機(jī)型
3. ConstraintLayout使用方法
3.1相對(duì)定位
相對(duì)定位是在ConstraintLayout中創(chuàng)建布局的基本構(gòu)件之一。這些約束允許控件
將給定的控件相對(duì)于另一個(gè)控件進(jìn)行定位。您可以約束一控件在水平方向和者
垂直方向:
Horizontal 方向: left, right, start and end sides
Vertical 方向: top, bottom sides and text baseline
文字描述比較抽象見(jiàn)下圖
代碼如下:
app:layout_constraintLeft_toRightOf="@+id/buttonA" />
經(jīng)常用到約束屬性列表:
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
它們都采用另一個(gè)控件的引用id,或者父組件(父組件將引用父容器,即ConstraintLayout):
app:layout_constraintLeft_toLeftOf="parent" />
3.2Margins(邊距)
如果設(shè)置了margin,它們將應(yīng)用于相應(yīng)的約束(如上圖),將邊距強(qiáng)制設(shè)置為目標(biāo)和源端之間的空間。通常的布局邊距屬性可以這樣使用:
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
注意:邊界只能是正的或等于0,具體值dp。
Margins visiable=gone的控件
當(dāng)位置約束目標(biāo)的可見(jiàn)性為gone,可以使用以下屬性指定一個(gè)margin值:
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom
3.3 Centering positioning(居中偏移)
ConstraintLayout水平居中顯示寫(xiě)法如下圖
控件中居中的寫(xiě)法為:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
意思是把控件的上下左右約束在布局的上下左右,這樣就能把控件放在
布局的中間了。相當(dāng)于RelativeLayout centerInParent=true
控件居中也可以添加偏差屬性進(jìn)行調(diào)整定位例如:ayout_marginLeft=”100dp“
居中后使用marginLeft=100dp,button水平居中后向右偏移100dp除了這種
還可以使用Bias(比例)
layout_constraintHorizontal_bias
layout_constraintVertical_bias
layout_constraintHorizontal_bias取值范圍為0-1,如果是0,則Button
在布局的最左側(cè),如果賦值1則Button在布局的最右側(cè),如果為0.5
則是水平居中,如果是0.3,則傾向于左側(cè)。垂直偏移同理
3.4 Circular positioning(圓心偏移)
約束一個(gè)控件中心相對(duì)于另一個(gè)控件中心的角度和距離。
將控件定位在圓上(如下圖)。
layout_constraintCircle : references another widget id
layout_constraintCircleRadius : the distance to the other widget center
layout_constraintCircleAngle : which angle the widget should be at
(in degrees, from 0 to 360)
3.5 Visibility behavior(隱藏行為)
ConstraintLayout會(huì)對(duì)標(biāo)記為View.GONE的控件進(jìn)行的特定處理。
設(shè)置Gone的控件不會(huì)顯示也不是布局本身的一部分.
如果標(biāo)記為gone他們的尺寸不會(huì)改變,但在布局計(jì)算方面,
gone的控件仍然是它的一部分,有一個(gè)重要的區(qū)別:
對(duì)于布局傳遞,它們的維度將被認(rèn)為是零(基本上,它們將被解析為一個(gè)點(diǎn))
如果它們對(duì)其他控件有約束,它們?nèi)匀粫?huì)有用,但任何邊界都將等于零
3.6 Dimension constraints(尺寸約束)
控件的尺寸可以通過(guò)四種不同方式指定:
1.使用指定的尺寸
2.使用wrap_content,讓控件自己計(jì)算大小
當(dāng)控件的高度或?qū)挾葹閣rap_content時(shí),可以使用下列屬性來(lái)控制最大、最小的高度或?qū)挾?#xff1a;
android:minWidth 最小的寬度
android:minHeight 最小的高度
android:maxWidth 最大的寬度
android:maxHeight 最大的高度
注意!當(dāng)ConstraintLayout為1.1版本以下時(shí),使用這些屬性需要加上強(qiáng)制約束,如下所示:
app:constrainedWidth=”true”
app:constrainedHeight=”true”
3. 使用 0dp (MATCH_CONSTRAINT)
官方不推薦在ConstraintLayout中使用match_parent,可以設(shè)置 0dp (MATCH_CONSTRAINT) 配合約束代替match_parent,舉個(gè)例子:
4. 寬高比
當(dāng)寬或高至少有一個(gè)尺寸被設(shè)置為0dp時(shí),可以通過(guò)屬性
layout_constraintDimensionRatio設(shè)置寬高比,
除此之外,在設(shè)置寬高比的值的時(shí)候,還可以在前面加W或H,分別指定寬度或高度限制。
例如:app:layout_constraintDimensionRatio="H,2:3"指的是 高:寬=2:3
app:layout_constraintDimensionRatio="W,2:3"指的是 寬:高=2:3
5. Percent dimension
要使用Percent,您需要設(shè)置以下內(nèi)容:
1.寬高設(shè)置為MATCH_CONSTRAINT (0dp)
2.默認(rèn)值應(yīng)該設(shè)置為% app:layout_constraintWidth_default="percent"或app:layout_constraintHeight_default="percent"
3.layout_constraintWidth_percent或layout_constraintHeight_percent屬性設(shè)置為0到1之間的值
3.7 Chains(鏈)
Chains在單個(gè)軸(水平或垂直)中提供類(lèi)似于組的行為。另一個(gè)軸可以獨(dú)立約束。
1.create chains
如果一組控件通過(guò)雙向連接鏈接在一起,那么它們就被認(rèn)為是一個(gè)鏈(如圖)
2.chains heads
鏈?zhǔn)怯稍O(shè)置在鏈的第一個(gè)元素(鏈的“頭”)上的屬性控制的:如下圖
head是水平鏈最左邊的控件,垂直鏈最上面的控件
3.margins in chains
如果在chains上指定了margins ,則會(huì)考慮到margins。在spread chains的情況下,margins 將從分配的空間中扣除。
4.Chain Style
在第一個(gè)控件上設(shè)置屬性layout_constraintHorizontal_chainStyle或者
layout_constraintVertical_chainStyle默認(rèn)是值是CHAIN_SPREAD
CHAIN_SPREAD:默認(rèn)模式
Weighted chain:如果一些控件被設(shè)置為MATCH_CONSTRAINT,它們將分割可用空間
CHAIN_SPREAD_INSIDE:chain的兩端不會(huì)被展開(kāi)
CHAIN_PACKED:該鏈的控件將被打包在一起。子元素的水平或垂直偏差屬性將影響打包元素的位置
4.Virtual Helpers objects(輔助工具)
4.1 Optimizer(計(jì)算優(yōu)化)
可以通過(guò)將設(shè)置: ConstraintLayout 屬性layout_optimizationLevel來(lái)進(jìn)行計(jì)算優(yōu)化
取值:none : 無(wú)優(yōu)化
standard : 只優(yōu)化直接約束和障礙約束
direct : 優(yōu)化直接約束
barrier : 優(yōu)化屏障約束
chain : 優(yōu)化鏈約束
dimensions : 優(yōu)化尺寸測(cè)量
4.2 Barrier(屏障)
barrierDirection取值:BOTTOM END LEFT RIGHT START
4.3 Group
該類(lèi)控制一組引用控件的可見(jiàn)性。控件通過(guò)添加到逗號(hào)分隔的id列表來(lái)引用
<androidx.constraintlayout.widget.Group
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:constraint_referenced_ids="TextView1,TextView2"/>
4.4 GuideLine
Guildline像輔助線一樣,在預(yù)覽的時(shí)候幫助你完成布局(不會(huì)顯示在界面上)。
Guildline的主要屬性:
android:orientation 垂直vertical,水平horizontal
layout_constraintGuide_begin 開(kāi)始位置
layout_constraintGuide_end 結(jié)束位置
layout_constraintGuide_percent 距離頂部的百分比(orientation= horizontal時(shí)則為距離左邊)
?4.5 Placeholder
Placeholder指的是占位符。在Placeholder中可使用setContent()設(shè)置另一個(gè)控件的id,使這個(gè)控件移動(dòng)到占位符的位置。
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的layout布局_Android ConstraintLayout 降低布局层次,布局优化首选的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jupyter 代码自动补全_在 Pyc
- 下一篇: python 文本处理模块_Python