約束布局常用屬性和輔助組件

一、常用屬性

1.相對定位屬性:layout_constraint X _to Y Of

一句話描述:當前控件的X邊界 - 在 - 目標控件的Y邊界
layout_constraintTop_toBottomOf
layout_constraintTop_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toEndOf
layout_constraintEnd_toStartOf
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintBaseline_toBaselineOf
一個控件最少需要 兩個 相對位置屬性 來確定它的具體位置

2.尺寸控制屬性:0dp

寬度和高度如果設置為0dp的話,控件就會撐滿能夠達到的最大寬高。
類似于線性布局里面加了權重的View。

3.居中和偏移屬性 :bias

舉例
app:layout_constraintHorizontal_bias="0.3"
這邊bias=0.3的意思是,左邊距離是左右總間距的30%。
假如bias=1的話,控件將緊靠右邊界。
假如bias>1的話,控件將超出右邊界。
(垂直情況下bias的值是上邊距占比)

4.鏈條布局屬性 :Chains

同一個方向(水平或者垂直)上的多個子 View 提供一個類似群組的概念。
該屬性添加到群組View的頭部子View即可:水平群組,最左邊的為頭, 垂直群組最上面為頭。
layout_constraintHorizontal_chainStyle = "packed"
layout_constraintHorizontal_weight
layout_constraintVertical_chainStyle
layout_constraintVertical_weight
具體的Style值 有三種:
packed
spread
spread_inside

二、輔助控件

1.參照線控件Guideline

參照線就是一個用來對齊其他視圖且運行時隱藏的參照視圖
app:orientation="vertical"聲明參照線是垂直還是水平方向
app:layout_constraintGuide_begin="41dp"表示參照線離父布局ConstraintLayout的起始位置為41dp,再次聲明,是start而不是left。
app:layout_constraintGuide_end=""表示相對于右邊緣的距離,
對于百分比參照線來說,使用app:layout_constraintGuide_percent="0.5"來描述百分比的偏移量。

2.分組控件:Group

組的作用是可將多個控件一起隱藏或顯示。
假如在代碼中實例化group控件,然后調(diào)用顯示隱藏方法。
app:constraint_referenced_ids 里面對應id的view 會一起隱藏或者顯示。

3.占位符控件:Placeholder

控件需要移動時的終點
下圖例子中Placeholder位于屏幕中心。
<androidx.constraintlayout.widget.Placeholder
android:id="@+id/placeholder"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

按鈕點擊后:將左上角圖片設置到占位符上。
fun click(view: View) {
//設置動畫
TransitionManager.beginDelayedTransition(constraintLayout)
//占位填充
placeholder?.setContentId(R.id.logo)
}

4.屏障控件:Barrier

<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="textView1,textView2"/>

控件A,控件B長度自適應,需要控件C一直在右邊,不管A和B誰長誰短 C均在右邊。
app:constraint_referenced_ids = "id1,id2 " 對應id的控件都會被屏障控件隔開.
app:barrierDirection="right" 這個屬性的意思是:屏障位于指定控件們的右邊。

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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