一、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>