關(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