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>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值