ConstraintLayout使用

前言

本來不想寫這篇文章的,之前看了一些關(guān)于ConstraintLayout的介紹。感覺使用上應(yīng)該沒什么問題,真正用起來還是有好多屬性記不清。Android的知識很零碎,還是要記錄一下

基本屬性

layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf

這些屬性和RelativeLayout的屬性類似,基本從字面就能理解是什么意思

寬高屬性

和其他布局一樣,可以用llayout_widthlayout_height設(shè)置寬高??梢允褂?code>wrap_content match_parent 或者具體的值。這里需要注意的 0dp

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="300dp"
        android:layout_height="0dp"
        android:scaleType="center"
        android:src="@drawable/icon_head_hydra_5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

以上布局指定寬度300,高度0。效果如下

image

以上這種情況0dp會直接充滿屏幕,效果=match_parent ,但是0dp也是受到條件約束的

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/icon_head_hydra_5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/iv2"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:layout_marginLeft="10dp"
        android:scaleType="centerCrop"
        android:src="@drawable/icon_head_hydra_4"
        app:layout_constraintBottom_toBottomOf="@+id/iv1"
        app:layout_constraintLeft_toRightOf="@+id/iv1"
        app:layout_constraintTop_toTopOf="parent" />
    
</androidx.constraintlayout.widget.ConstraintLayout>
image

這里指定的iv1的寬高,iv2的底部和頂部都和iv1對齊。那自然iv2的高度也要和iv1一致

寬高比

一般在做banner的會遇到這樣的需求

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        android:src="@drawable/icon_hydra_11"
        app:layout_constraintDimensionRatio="H,16:9"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
image

寬高指定為0dp,app:layout_constraintDimensionRatio="H,16:9"

layout_constraintDimensionRatio 表示期望的寬高比,不僅可以用來表示寬高比, 也可以用來表示高寬比

app:layout_constraintDimensionRatio的值里面的H和W是什么意思。加上h的意思就是,h之后的比例是以w為基礎(chǔ)去設(shè)置h,即h = w * ratio。w的意思是,w = h / ratio (因為 ratio = w / h 代表寬高比)

不寫H,也不寫W的情況下, 表示 寬高比
寫了H 和 不寫H 效果是一樣的,都是 表示 寬高比

權(quán)重

一般底部的app都是平分寬度,用LinearLayout很容易實現(xiàn)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tab1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="Tab1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tab2" />


    <TextView
        android:id="@+id/tab2"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="Tab2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/tab1"
        app:layout_constraintRight_toLeftOf="@+id/tab3" />


    <TextView
        android:id="@+id/tab3"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimaryDark"
        android:gravity="center"
        android:text="Tab3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/tab2"
        app:layout_constraintRight_toRightOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
image

這里三個TextView的寬度都是0并且左右相互依賴,這樣就平分整個屏幕的寬度

單獨設(shè)置比例

layout_constraintHorizontal_weight layout_constraintVertical_weight

這兩個屬性相當于LinearLayout的weight

image

bias

bias可以看成一個偏移量,個人感覺實際的應(yīng)用場景應(yīng)該不多

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        app:layout_constraintHorizontal_bias="0.1"
        app:layout_constraintVertical_bias="0.1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:src="@drawable/icon_head_hydra_5"
        android:layout_width="200dp"
        android:layout_height="200dp" />


</androidx.constraintlayout.widget.ConstraintLayout>
image

原本ImageView應(yīng)該是處于屏幕居中的

app:layout_constraintHorizontal_bias="0.1" 橫向偏移1/10的距離(左側(cè)邊際的距離)

app:layout_constraintVertical_bias="0.1" 水平編譯1/10的距離(頂部邊際的距離)

這里需要注意的是:
想要設(shè)置偏移,必須先將控件設(shè)置父布局居中。
偏移的長度,如橫向的偏移,是父布局寬度減去ImageView寬度的剩下的10%

百分比

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        android:src="@drawable/icon_head_hydra_5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHeight_percent="0.3"
        app:layout_constraintWidth_percent="0.5" />


</androidx.constraintlayout.widget.ConstraintLayout>
image

app:layout_constraintHeight_percent="0.3" ImageView的高度是屏幕的0.3

app:layout_constraintWidth_percent="0.5" ImageView的寬度是屏幕的0.5

百分比默認相對于屏幕,可以通過以下屬性改變參照物

app:layout_constraintWidth_default="percent"

chain

先看圖

image

這張圖包含了鏈的三種類型

  • packed:將view放在一起,沒有間距
  • spread:view均勻分布,間距一直
  • spread_inside 兩端定頭沒有間距,中間居中

要使用約束鏈,控件必須先建立約束關(guān)系。每個控件之間相互依賴

上圖中的代碼

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/packed"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="packed"
        android:textColor="#000"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/packedtab1"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="Tab1"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/packedtab2"
        app:layout_constraintTop_toBottomOf="@+id/packed" />


    <TextView
        android:id="@+id/packedtab2"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="Tab2"
        app:layout_constraintLeft_toRightOf="@id/packedtab1"
        app:layout_constraintRight_toLeftOf="@+id/packedtab3"
        app:layout_constraintTop_toBottomOf="@+id/packed" />


    <TextView
        android:id="@+id/packedtab3"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimaryDark"
        android:gravity="center"
        android:text="Tab3"
        app:layout_constraintLeft_toRightOf="@id/packedtab2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/packed" />


    <TextView
        android:id="@+id/spread"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="spread"
        android:textColor="#000"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/packedtab3" />


    <TextView
        android:id="@+id/spreadtab1"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="Tab1"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/spreadtab2"
        app:layout_constraintTop_toBottomOf="@+id/spread" />


    <TextView
        android:id="@+id/spreadtab2"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="Tab2"
        app:layout_constraintLeft_toRightOf="@id/spreadtab1"
        app:layout_constraintRight_toLeftOf="@+id/spreadtab3"
        app:layout_constraintTop_toBottomOf="@+id/spread" />


    <TextView
        android:id="@+id/spreadtab3"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimaryDark"
        android:gravity="center"
        android:text="Tab3"
        app:layout_constraintLeft_toRightOf="@id/spreadtab2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spread" />


    <TextView
        android:id="@+id/spread_inside"
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="spread_inside"
        android:textColor="#000"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spreadtab3" />


    <TextView
        android:id="@+id/spread_inside_tab1"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="Tab1"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/spread_inside_tab2"
        app:layout_constraintTop_toBottomOf="@+id/spread_inside" />


    <TextView
        android:id="@+id/spread_inside_tab2"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="Tab2"
        app:layout_constraintLeft_toRightOf="@id/spread_inside_tab1"
        app:layout_constraintRight_toLeftOf="@+id/spread_inside_tab3"
        app:layout_constraintTop_toBottomOf="@+id/spread_inside" />


    <TextView
        android:id="@+id/spread_inside_tab3"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimaryDark"
        android:gravity="center"
        android:text="Tab3"
        app:layout_constraintLeft_toRightOf="@id/spread_inside_tab2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spread_inside" />


</androidx.constraintlayout.widget.ConstraintLayout>

輔助線GuideLine

輔助線有橫向的和縱向的,輔助線不會顯示到界面上。具體的用法就是在布局中添加輔助線,用輔助線來控制view的位置。

屬性:

android:orientation 這個一看就知道是指定方向

layout_constraintGuide_begin 距離頂部的位置

layout_constraintGuide_end 距離底部的位置

layout_constraintGuide_percent 距離頂部距離的百分比

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/icon_head_hydra_5"
        app:layout_constraintLeft_toRightOf="@+id/guidLine"
        app:layout_constraintTop_toTopOf="parent" />


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="50dp" />


</androidx.constraintlayout.widget.ConstraintLayout>
image

這里創(chuàng)建了一個垂直的輔助線,距離左邊屏幕50dp。ImageView在輔助線的右邊。這是如果移動輔助線,ImageView也會跟著移動。

Barrier

BarrierGuideline一樣,不會被顯示 。Barrier 包裹了一些控件,它的寬高由包裹的子控件決定。

效果如下圖

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:padding="10dp">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:src="@drawable/icon_hydra_9"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/barrier"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="HailHydra"
        android:textColor="@color/color_black"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="阿姆斯特朗回旋加速噴氣式阿姆斯特朗炮"
        android:textColor="@color/color_black"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_name" />

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


</androidx.constraintlayout.widget.ConstraintLayout>
image

當右側(cè)文字長度發(fā)生變化,左側(cè)的圖片位置也會發(fā)生變化

Group

顯示或者隱藏多個控件,一般我都是直接用個大布局把他們包起來。在constraintlayout中可以使用Group。用法和Barrier差不多。但是Group沒有寬高的概念,所以無法作為其他view的約束參照物。隱藏的時候直接找到控件指定Gone就可以了

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">


    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="阿姆斯特朗回旋加速噴氣式阿姆斯特朗炮"
        android:textColor="@color/color_black"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_name" />


    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="HailHydra"
        android:textColor="@color/color_black"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <androidx.constraintlayout.widget.Group
        android:id="@+id/group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="tv_name,tv_content" />


</androidx.constraintlayout.widget.ConstraintLayout>

Placeholder 占位

laceholder指的是占位符。在Placeholder中可使用setContent()設(shè)置另一個控件的id,使這個控件移動到占位符的位置

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <androidx.constraintlayout.widget.Placeholder
        android:id="@+id/placeholder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:content="@+id/textview"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#cccccc"
        android:padding="16dp"
        android:text="TextView"
        android:textColor="#000000"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

當Placeholder沒有指定content的時候

image

當Placeholder指定content的時候

image

注意

在constraintlayout盡量不要使用android:visibility="gone" 一旦這個view消失,那所有跟它有約束關(guān)系的view都會消失??梢允褂?code>invisible代替

資料

ConstraintLayout 完全解析 快來優(yōu)化你的布局吧
ConstraintLayout使用指南
ConstraintLayout已經(jīng)2.0了,你不來了解一下嗎?

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

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

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