一、概述:
ConstraintLayout允許您使用平面視圖層次結構(無嵌套視圖組)創建大而復雜的布局。 它與RelativeLayout類似,所有的視圖都是根據兄弟視圖和父布局之間的關系來布局的,但是它比RelativeLayout更靈活,并且更易于在Android Studio的布局編輯器中使用。ConstraintLayout的所有功能都可以直接從布局編輯器的可視化工具中使用,因為布局API和布局編輯器是專門為對方構建的。 所以你可以使用ConstraintLayout完全通過拖放操作來構建你的布局,而不是編輯XML。
二、自己的操作:手寫各約束布局屬性
1、首先需要引入我們的ConstraintLayout,在build.gradle中加入:(現在創建應用默認加入)
compile'com.android.support.constraint:constraint-layout:1.0.2'
2、我們使用ConstraintLayout來寫
tv1設置了://父布局的左上角
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tv2設置了:tv2在tv1的右側,tv2的右側和父布局對其,tv2和tv1頂部對齊;
app:layout_constraintLeft_toRightOf="@id/tv1",
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/tv1"
tv3設置了:tv3在tv1的右側,tv3和tv1底部對其。
app:layout_constraintLeft_toRightOf="@id/tv1"
app:layout_constraintBottom_toBottomOf="@id/tv1"
與之類似的還有幾個屬性:
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
總體來說如果嵌套很多層的使用這個替換還是有必要的,就是簡單的玩了一下等后面做復雜布局再繼續完善吧
三、API 參考文檔:
四、來自大牛的啟蒙:
可視化界面學習推薦:郭霖:Android新特性介紹,ConstraintLayout完全解析
手寫各約束布局屬性:鴻洋:ConstraintLayout 完全解析 快來優化你的布局吧