ConstraintLayout控件很強大,現在基本上寫佈局都是用它寫的,簡單記錄下幾個常用api
相對定位
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
這裏主要講一下layout_constraintBaseline_toBaselineOf怎麼使用。什麼是baseline,貼張官方圖
我在第一次使用的時候,左邊控件沒有baseline(eg:ImageView),導致右邊控件無法對齊,一度認爲該api沒卵用。
邊距
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom
前面幾個就不用過多介紹,後面帶gone屬性很厲害,拿layout_goneMarginStart舉例,假設控件A在父容器左邊20dp,控件B在A右邊10dp,並且設置了layout_goneMarginStart=20dp,那麼當A顯示的時候,AB水平間距=10dp,當A隱藏的時候,B在父容器左邊20dp。
鏈(Chain)
layout_constraintHorizontal_chainStyle 水平方向鏈式
layout_constraintVertical_chainStyle 垂直方向鏈式
它兩的值默認可以是
CHAIN_SPREAD 展開樣式(默認)
Weighted chain 在CHAIN_SPREAD樣式,部分控件設置了MATCH_CONSTRAINT,那他們將擴展可用空間。
CHAIN_SPREAD_INSIDE 展開樣式,但兩端不展開
CHAIN_PACKED 抱團(打包)樣式,控件抱團一起。通過偏移bias,可以改變packed元素的位置。