深入分析ConstraintLayout的原理及应用场景(1)

//在父布局的下方
app:layout_constraintTop_toBottomOf=“parent”

//在父布局的右方
app:layout_constraintStart_toEndOf=“parent”

//在父布局的左方
app:layout_constraintEnd_toStartOf=“parent”

使用前导包
  1. 在顶级build.gradle文件中

repositories {
google()
}

  1. 模块级build.gradle

// 尽可能地下载最新版本,如果不确定最新版本号,可以先写入1.0.0,系统会标注提醒最新版本号.
dependencies {
implementation “androidx.constraintlayout:constraintlayout:2.0.4”
}

使用场景
  • 场景a:A控件与“222”控件居中对齐,且“222”控件在A控件下方。如图(即基于某控件的一边,居中对齐)

约束布局如图.png

代码实现如下:

<?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”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.MainActivity”>

//因为要位于下方,所以使用layout_constraintTop_toBottomOf属性;
//同时左右两边要基于另一控件对齐(因为要居中)

</androidx.constraintlayout.widget.ConstraintLayout>

  1. 居中用法引申:同一维度(上下或左右)的两个方向同时出现,且相对于父布局对齐。
  2. 非居中用法引申:若不居中对齐,而是基于某一边对齐,只需去掉同一个维度的某一个方向。例如,上方代码中,去掉app:layout_constraintEnd_toEndOf=“@id/tv1”, 即实现A与“222”控件左方对齐,且A在“222”控件上方。
  • 场景2:六个控件在布局中以三行三列形式分布,且行均分布局高度,列均分布局宽度。如图。

ConstraintLayout场景2.png

核心实现流程如下:

  1. 每一个相对的控件,都要写出相约束的属性(比如layout_constraintEnd_toStartOf;layout_constraintStart_toEndOf)。
  2. 每个控件四个方向的约束位置都要写出来。
  3. 每一个横向或竖向位置的两端,必须与parent相对。

// 横向:
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintEnd_toEndOf=“parent”;

// 竖向:
app:layout_constraintTop_toTopOf=“parent”;
app:layout_constraintBottom_toBottomOf=“parent”;

  1. 利用权重实现均分(weight属性)
  2. 均分时,长或宽都必须为0。

代码如下所示。

<?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”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.MainActivity”>

/*
核心代码
*/

</androidx.constraintlayout.widget.ConstraintLayout>

  • 场景3:ConstraintSet的动画效果实现

代码实现如下。

架构师筑基包括哪些内容

我花了将近半个月时间将:深入 Java 泛型.、注解深入浅出、并发编程.、数据传输与序列化、Java 虚拟机原理、反射与类加载、高效 IO、Kotlin项目实战等等Android架构师筑基必备技能整合成了一套系统知识笔记PDF,相信看完这份文档,你将会对这些Android架构师筑基必备技能有着更深入、更系统的理解。

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容

注:资料与上面思维导图一起看会更容易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!



这份资料就包含了所有Android初级架构师所需的所有知识!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!**

[外链图片转存中…(img-m36bqOum-1715336202675)]
[外链图片转存中…(img-0jB5is6l-1715336202676)]
这份资料就包含了所有Android初级架构师所需的所有知识!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值