天天看點

ConstraintLayout的使用一、ConstraintLayout是什麼?二、使用步驟

一、ConstraintLayout是什麼?

示例:大家都知道,當布局嵌套深入比較深的時候,往往會伴随着一些性能問題。是以很多時候我們建議使用RelativeLayout或者GridLayout來簡化掉布局的深度。

而對于簡化布局深度,ConstraintLayout幾乎可以做到極緻,接下來我們通過執行個體來盡可能将所有常見的屬性一步步的介紹清楚。看了很多教程都是拖拽的很難受,于是決定整個手寫的簡單示例供大家參考

二、使用步驟

1.引入庫

代碼如下(示例):

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#11ff0000">
    /**
    tv_1 是黃色圖案 目的是讓它在左上角
    我們用到了 這兩個限制,
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
    這兩個限制可以了解為1、自己的上邊和父布局的上面對齊
    2、自己的左邊和父布局左邊對齊
        
    **/ 
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="80dp"
        android:layout_height="90dp"
        android:background="#FFEB3B"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        //自己左邊和tv_1的右邊對齊
        app:layout_constraintLeft_toRightOf="@+id/tv_1"
        android:text="測試"
        android:gravity="center"
        //自己右邊和父布局右邊對齊
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/tv_1"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="das"
        自己左邊和tv_1右邊對齊
        app:layout_constraintLeft_toRightOf="@+id/tv_1"
        自己下面和tv_1下面對齊
        app:layout_constraintBottom_toBottomOf="@+id/tv_1"/>



    <TextView
        android:id="@+id/tab1"
        app:layout_constraintHorizontal_weight="1"
        android:layout_width="0dp"
        android:layout_marginLeft="30dp"
        android:layout_height="0dp"
        android:background="#f67"
        android:gravity="center"
        android:text="Tab1"
        app:layout_constraintTop_toTopOf="@+id/gl_2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tab2" />


    <TextView
        android:id="@+id/tab2"
        app:layout_constraintHorizontal_weight="1"
        android:layout_marginLeft="30dp"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#A67"
        android:gravity="center"
        android:text="Tab2"
        app:layout_constraintTop_toTopOf="@+id/gl_2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/tab1"
        app:layout_constraintRight_toLeftOf="@+id/tab3" />


    <TextView
        android:id="@+id/tab3"
        app:layout_constraintHorizontal_weight="1"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#767"
        android:gravity="center"
        android:text="Tab3"
        app:layout_constraintTop_toTopOf="@+id/gl_2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/tab2"
        app:layout_constraintRight_toRightOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"/>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/gl_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        畫一條水準的參考線0.9代表百分比
        app:layout_constraintGuide_percent="0.9"/>
</androidx.constraintlayout.widget.ConstraintLayout>

           
ConstraintLayout的使用一、ConstraintLayout是什麼?二、使用步驟