1.目錄
- 布局嵌套減少,提高性能,具體可點(diǎn)擊跳轉(zhuǎn)查看
- 正文背景
- 百分比布局
- 寬高比布局
- goneMargin
- Group
- 居中和偏移
- 居中和偏移
- 360度無死角定位布局
- Barrier
- GuideLine
- PlaceHolder
- ConstraintSet
- 官方文檔鏈接
- 我說
2.正文背景
約束布局顧名思義ConstraintLayout里的子view通過一些約束來確定自己的顯示(位置,大小等)繁扎。
子view上下左右的約束屬性有以下幾個(gè)桨醋,子view的約束對(duì)象可以是其他子view泪电,也可以是ConstraintLayout缀程,因此以下屬性的值可以是其他子view的id诲泌,也可以是"parent"表示ConstraintLayout
layout_constraintLeft_toLeftOf ===> 子view左邊受哪個(gè)view的左邊的約束,如此類推
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
layout_constraintBaseline_toBaselineOf
ConstraintLayout和其他布局下的子view的寬高屬性值略有不同妻坝。主要體現(xiàn)在充滿父布局的屬性值不同伸眶,其他布局的充滿父布局屬性為match_parent和約束布局的為match_constraint(即0dp)
注意:match_constraint的表現(xiàn)受layout_constraintXXX_default值的影響
==spread:充滿約束空間 默認(rèn)情況
==wrap:匹配內(nèi)容大小但不超過約束限制
==percent:百分比布局
3.百分比布局:屏幕適配
前提條件:width或height必須為match_constraint(即0dp)惊窖,否則百分比布局無效
使用方式:android:layout_width="0dp" app:layout_constraintWidth_default="percent" app:layout_constraintWidth_percent="1"
4.寬高比
app:layout_constraintDimensionRatio="w,1:2"
如果要約束w(寬)(即高是定的),w開頭赚抡,寬高比爬坑;h開頭,高寬比涂臣。
如果要約束h(高)(即寬是定的)盾计,h開頭,寬高比赁遗;w開頭署辉,高寬比。
如果寬高同時(shí)為0dp(寬高都可定的話)岩四,那么w開頭哭尝,表示高定約束寬,寬高比剖煌;h開頭材鹦,表示寬定約束高,寬高比
5.goneMargin:約束view消失后才起作用的Margin屬性
6.Group統(tǒng)一控制多個(gè)控件的顯示或隱藏耕姊。group的visibility屬性比view本身的優(yōu)先級(jí)最高桶唐,而且constraint_referenced_ids只能引用約束布局下的直接子view的id。
7.居中和偏移
8.鏈?zhǔn)讲季?/h5>
9.360度無死角定位布局
10.Barrier與多個(gè)控件集合的某邊對(duì)齊
11.GuideLine茉兰,當(dāng)沒有合適約束view的時(shí)候尤泽,可以創(chuàng)建一個(gè)guideline作為約束view
12.PlaceHolder 用placeholder中的約束條件替換對(duì)應(yīng)id的view的約束條件
應(yīng)用場(chǎng)景:作為位置模板尚氛,引入后只需要寫內(nèi)容view产园;通過代碼動(dòng)態(tài)改變內(nèi)容,結(jié)合TransitionManager可以做一些有趣的過渡動(dòng)畫等