ConstraintLayout使用

關(guān)于ConstraintLayout的介紹官方文檔給出了如下解釋:
ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It's similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayoutand easier to use with Android Studio's Layout Editor.
大致意思就是:ConstraintLayout允許您使用平面視圖層次結(jié)構(gòu)創(chuàng)建大型復(fù)雜布局(無嵌套視圖組)。 它與RelativeLayout類似,因?yàn)樗幸晥D都根據(jù)兄弟視圖和父布局之間的關(guān)系進(jìn)行布局,但它比RelativeLayout更靈活,更易于與Android Studio的布局編輯器一起使用。說白了就是它的出現(xiàn)就是解決布局嵌套層次出現(xiàn)的一種高級(jí)布局,并且現(xiàn)在ConstraintLayout也是AndroidStudio默認(rèn)生成的布局。

下面就是介紹它的一些屬性:
app:layout_constraintStart_toStartOf="parent或者@+id/控件id" 當(dāng)前控件的開始位置相對(duì)于另一個(gè)控件的開始位置,同理下面也是同樣不在重復(fù)介紹
app:layout_constraintStart_toEndOf="parent或者@+id/控件id"
app:layout_constraintTop_toTopOf="parent或者@+id/控件id"
app:layout_constraintTop_toBottomOf="parent或者@+id/控件id"
app:layout_constraintEnd_toEndOf="parent或者@+id/控件id"
app:layout_constraintEnd_toStartOf="parent或者@+id/控件id"
app:layout_constraintBottom_toBottomOf="parent或者@+id/控件id"
app:layout_constraintBottom_toTopOf="parent或者@+id/控件id"

?水平?方向居中:

app:layout_constraintStart_toStartOf="@+id/控件id"
app:layout_constraintEnd_toEndOf="@+id/控件id"

垂直?方向居中:

app:layout_constraintTop_toTopOf="@+id/控件id"
app:layout_constraintBottom_toBottomOf="@+id/控件id"

控件垂直居中于 view 的下邊:

app:layout_constraintTop_toBottomOf="@+id/控件id"
app:layout_constraintBottom_toBottomOf="@+id/控件id"

權(quán)重設(shè)置

例如水平向的控件設(shè)置權(quán)重,?大?小為 2:1:1 :
首先各個(gè)自 view需要在水平方向上穿起來,一個(gè)連接一個(gè)然后

android:layout_width="0dp"
app:layout_constraintHorizontal_weight="2"

android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"

android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"

?文字基準(zhǔn)線對(duì)?齊:

app:layout_constraintBaseline_toBaselineOf="@+id/控件id"

圓形定位功能:

app:layout_constraintCircle="@+id/view" 圓心
app:layout_constraintCircleAngle="90" 角度
app:layout_constraintCircleRadius="180dp" 圓的半徑

限制控件?大?小不不會(huì)超過約束范圍:

app:layout_constrainedWidth="true"
app:layout_constrainedHeight="true"

控制控件在垂直?方向的 30%的位置:

app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.3"

約束鏈

在約束鏈上的第?一個(gè)控件上加上 chainStyle ,?用來改變?一組控件的布局?方式
packed(打包)
spread (擴(kuò)散) 默認(rèn)樣式
spread_inside(內(nèi)部擴(kuò)散)

垂直?方向 packed:
app:layout_constraintVertical_chainStyle="packed"

百分?比布局

需要對(duì)應(yīng)?方向上的值為 match_constraint(0)
百分?比是parent 的百分?比,?而不不是約束區(qū)域的百分?比
例如:
寬度是?父容器?的 30%:
android:layout_width="0dp"
app:layout_constraintWidth_percent="0.3"

輔助控件

GuideLine
設(shè)置輔助線的?方向 android:orientation="vertical"
設(shè)置輔助線的位置,根據(jù)?方向不不同
距離左側(cè)或上側(cè)的距離 layout_constraintGuide_begin
距離右側(cè)或下側(cè)的距離 layout_constraintGuide_end
百分? layout_constraintGuide_percent 位置是相對(duì)于父容器

Barrier

通過設(shè)置?一組控件的某個(gè)?方向的屏障,不讓越界,用來避免布局嵌套。
app:barrierDirection="end"http://屏障的方向(一共六個(gè)start,end,left,right,top,bottom)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="id1,id2"

Placeholder占位符

通過 setContentId 來將指定控件放到占位符的位置,通過加上 TransitionManager 來?自動(dòng)完成過渡動(dòng)畫。TransitionManager.beginDelayedTransition(父容器)

Group

通過 constraint_referenced_ids 使?用引?用的?方式來避免布局嵌套。
可以為?一組控件統(tǒng)?一設(shè)置 setVisibility

?著作權(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ù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ConstraintLayout在AS2.2時(shí)候就有了,2.3時(shí)代作為了AS EmptyActivity模板的默認(rèn)...
    ZHDelete閱讀 6,350評(píng)論 2 3
  • 傳統(tǒng)布局缺陷 ![]這里寫圖片描述 在這樣場景下發(fā)現(xiàn)最多的時(shí)候用到四層線性布局,共嵌套了五層,即使使用Relati...
    kinsomy閱讀 884評(píng)論 1 1
  • Android在遇到復(fù)雜布局的時(shí)候,避免不了嵌套布局,渲染起來也影響了應(yīng)用的性能,而且在維護(hù)的時(shí)候會(huì)有些煩躁,至少...
    aidlFor閱讀 9,848評(píng)論 0 38
  • ConstraintLayout是2016年Google I/O推出并重點(diǎn)宣傳的一個(gè)組件,在Android Stu...
    宛丘之上兮閱讀 44,153評(píng)論 10 43
  • 其實(shí)這東西剛出來的時(shí)候玩了會(huì)感覺太累,就放棄了。不過看這個(gè)google不會(huì)放棄這東西,咱還是在平時(shí)的demo練習(xí)中...
    有點(diǎn)健忘閱讀 5,745評(píng)論 0 3

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