ConstraintLayout 简介

知识回顾

Android 五大布局:

  1. 线性布局
  2. 相对布局
  3. 帧布局
  4. 网格布局
  5. 绝对布局

网格布局几乎没有用到过,绝对布局如果不是 Webview 继承与他,基本也被废弃了。

定义

Android第六大布局,约束布局,一定程度替代了前五大布局。

使用原则:
线性布局的效率还是值得肯定的,在不嵌套的前提下,如果用鲜血布局更简单还是推荐使用;完全代替相对布局。

入门使用

约束布局必须对当前view添加约束条件才能确认他的位置,至少在横向和纵向各添加一个约束条件

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
    
	<TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingVertical="20dp"
        android:textColor="#FF2D2942"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="提示" />
        
</androidx.constraintlayout.widget.ConstraintLayout>

例:
app:layout_constraintStart_toStartOf
layout_constraint:代表这是一个约束属性

app:layout_constraintStart_toStartOf
Start:代表当前这个控件的开始位置

app:layout_constraintStart_toStartOf
toStartOf:代表要约束到的控件的开始位置
下划线后表示目标控件

app:layout_constraintStart_toStartOf的value可以是父布局,也可以是具体的某一个view

所以:
app:layout_constraintStart_toStartOf=“parent”
意思是:我的开始的位置约束到父布局的开始位置

同样的道理:
app:layout_constraintEnd_toEndOf=“parent”
我的结束的位置约束到父布局的结束位置

app:layout_constraintTop_toTopOf=“parent”
我的头部的位置约束到父布局的头部位置

app:layout_constraintBottom_toBottomOf=“parent”
我的底部的位置约束到父布局的底部位置

然后排列组合:
app:layout_constraintStart_toEndOf=“parent”
我的开始的位置约束到父布局的结束位置

,,,,,,

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值