当前位置: 首页 > news >正文

从0开始写android

系列文章目录

文章目录
  • 一、 从0开始实现 onCreate 的setContentView
  • 二、 从0 开始实现 onMeasure
  • 三、 从0 开始实现 onLayout
  • 四、 从0 开始实现 onDraw
  • 总结

前言

接上文,测量完View树的每个节点View的宽和高后,开始布局。

一、ViewRootImpl 的调用栈

ViewRootImpl->performLayout(lp, mWidth, mHeight);

                        host.layout()

ViewGroup -> layout(int l, int t, int r, int b)

View -> layout(int l, int t, int r, int b)

View ->setFrame(l, t, r, b)

           onLayout(changed, l, t, r, b)

DecorView-> onLayout(changed, l, t, r, b)

FrameLayout->onLayout()

                        layoutChildren()

二、布局每个View

View.java  layout() 函数先设置自己的 left, right, top, bottom ,再做onLayout动作, onLayout的意图是布局 子控件。 view 已经没有子控件了, 所以 view的onLayout 什么也不做。

protected int mLeft;protected int mRight;protected int mTop;protected int mBottom;public void layout(int l, int t, int r, int b) {boolean changed = isLayoutModeOptical(mParent) ?setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {onLayout(changed, l, t, r, b);
}}protected boolean setFrame(int left, int top, int right, int bottom) {...mLeft = left;mTop = top;mRight = right;mBottom = bottom;...
}protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
}

由于 ViewGroup 的 onLayout 是虚函数, 所以 ViewGroup 需要实现自己的onLayout

   public final void layout(int l, int t, int r, int b) {if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) {if (mTransition != null) {mTransition.layoutChange(this);}super.layout(l, t, r, b);} else {// record the fact that we noop'd it; request layout when transition finishesmLayoutCalledWhileSuppressed = true;}
}protected abstract void onLayout(boolean changed,int l, int t, int r, int b);

总结:

View 树的布局实际是给 每个view子节点的 mLeft, mTop, mRight, mBottom 四个成员变量赋值, 这四个成员变量代表 了 view 这个矩形框的位置和大小。

http://www.lryc.cn/news/292350.html

相关文章:

  • 使用pygame建立一个简单的使用键盘方向键移动的方块小游戏
  • 从零开始:CentOS系统下搭建DNS服务器的详细教程
  • 2024美赛B题解析:寻找潜水器Searching for Submersibles
  • 回归预测 | Matlab基于POA-LSSVM鹈鹕算法算法优化最小二乘支持向量机的数据多输入单输出回归预测
  • 把 matlab 公式输出成 latex 公式形式
  • 云上自动部署丨使用 Terraform 在 AWS 上搭建 DolphinDB
  • vscode的ssh忽然连不上服务器:远程主机可能不符合glibc和libstdc++ VS Code服务器的先决条件
  • C++(17)——list的模拟实现
  • 花瓣网美女图片爬取
  • Android native层c++调用java层API
  • Docker 集群配置
  • VUE3+elementPlus 之 Form表单校验器 之 字符长度校验
  • 【Mysql】数据库架构学习合集
  • 轻型民用无人机驾驶航空器安全操控——理论考试多旋翼部分笔记
  • UE4学习笔记 FPS游戏制作3 添加武器
  • 详解 Prim 算法的实现
  • Android 使用高德地图
  • 从redis setnx 来看看分布式锁
  • 校园网网络规划与设计——计算机网络实践报告
  • Qt QScrollArea 不显示滚动条 不滚动
  • 【SVN在Linux下的常用指令】
  • 2024 高级前端面试题之 Node 「精选篇」
  • linux麒麟系统安装mongodb7.0
  • Spring声明式事务
  • PyTorch深度学习实战(34)——Pix2Pix详解与实现
  • 第96讲:MySQL高可用集群MHA的核心概念以及集群搭建
  • 外星人入侵(python)
  • Unity中开发程序打包发布
  • 2024.2.1日总结
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组