View->View测量布局中requestLayout和forceLayout的区别

XML文件

<?xml version="1.0" encoding="utf-8"?>
<com.gallery20.app.MyLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/my_ll"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/holo_green_light"android:gravity="center"android:orientation="vertical"><com.gallery20.app.MyViewandroid:id="@+id/my_view"android:layout_width="match_parent"android:layout_height="100dp"android:background="@android:color/holo_blue_light"/><com.gallery20.app.MyButtonandroid:id="@+id/my_btn"android:layout_width="match_parent"android:layout_height="100dp"android:background="@android:color/holo_red_light"/>
</com.gallery20.app.MyLinearLayout>

Activity文件

const val TAG = "Yang"
class MainActivity : AppCompatActivity() {private var mViewGroup : LinearLayout ?= nullprivate var mButton : Button ?= nullprivate var mView : View?= nullprivate var mMainHandler = Handler(Looper.getMainLooper())override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)mViewGroup = findViewById(R.id.my_ll)mButton = findViewById(R.id.my_btn)mView = findViewById(R.id.my_view)mMainHandler.postDelayed({Log.i("yang", "<-------requestLayout------->")// 请求重新布局mView?.requestLayout()}, 2000)}
}

自定义View代码

class MyButton(context: Context, attrs: AttributeSet) : AppCompatButton(context, attrs) {override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {super.onMeasure(widthMeasureSpec, heightMeasureSpec)Log.d(TAG, "MyButton onMeasure")}override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {super.onLayout(changed, l, t, r, b)Log.d(TAG, "MyButton onLayout")}override fun onDraw(canvas: Canvas?) {super.onDraw(canvas)Log.d(TAG, "MyButton onDraw")}
}class MyLinearLayout(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {super.onMeasure(widthMeasureSpec, heightMeasureSpec)Log.d(TAG, "MyLinearLayout onMeasure")}override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {super.onLayout(changed, l, t, r, b)Log.d(TAG, "MyLinearLayout onLayout")}override fun onDraw(canvas: Canvas?) {super.onDraw(canvas)Log.d(TAG, "MyLinearLayout onDraw")}
}class MyView(context: Context, attrs: AttributeSet) : View(context, attrs) {override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {super.onMeasure(widthMeasureSpec, heightMeasureSpec)Log.d(TAG, "MyView onMeasure")}override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {super.onLayout(changed, l, t, r, b)Log.d(TAG, "MyView onLayout")}override fun onDraw(canvas: Canvas?) {super.onDraw(canvas)Log.d(TAG, "MyView onDraw")}fun updateView() {val layoutParams = layoutParamslayoutParams.height += 100}
}

requestLayout()方法

  • 不修改View的布局参数
 mMainHandler.postDelayed({Log.i("yang", "<-------requestLayout------->")// 请求重新布局mView?.requestLayout()}, 2000)// log 
2024-06-09 08:42:24.081  4438-4438  yang                    I  <-------requestLayout------->
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyView onMeasure
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyLinearLayout onMeasure
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyView onLayout
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyLinearLayout onLayout
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyLinearLayout onDraw
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyView onDraw
  • 修改View的布局参数
mMainHandler.postDelayed({Log.i("yang", "<-------requestLayout------->")mView?.updateView()// 请求重新布局mView?.requestLayout()
}, 2000)// log
2024-06-09 08:50:21.373  4943-4943  yang                 I  <-------requestLayout------->
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyView onMeasure
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyLinearLayout onMeasure
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyView onLayout
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyButton onLayout
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyLinearLayout onLayout
2024-06-09 08:50:21.377  4943-4943  Yang                 D  MyLinearLayout onDraw
2024-06-09 08:50:21.377  4943-4943  Yang                 D  MyView onDraw
2024-06-09 08:50:21.384  4943-4943  Yang                 D  MyView onDraw 

forceLayout()方法

  • 不修改View的布局参数
mMainHandler.postDelayed({Log.i("yang", "<-------forceLayout------->")// 请求重新布局mView?.forceLayout()
}, 2000)// log 
没有改变View的可见性、改变View的大小或位置,无任何Log输出
  • 修改View的布局参数
mMainHandler.postDelayed({Log.i("yang", "<-------forceLayout------->")mView?.updateView()// 请求重新布局mView?.forceLayout()
}, 2000)// log
2024-06-09 08:57:53.732  5625-5625  yang                   I  <-------forceLayout------->
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyView onMeasure
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyLinearLayout onMeasure
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyView onLayout
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyButton onLayout
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyLinearLayout onLayout
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyLinearLayout onDraw
2024-06-09 08:57:53.742  5625-5625  Yang                   D  MyView onDraw
2024-06-09 08:57:53.748  5625-5625  Yang                   D  MyView onDraw

总结

  • forceLayout()requestLayout()的主要区别在于影响范围:
    • forceLayout()将当前View标记为需要重新布局,但并不会立即触发布局过程。只有在下次draw(Canvas)方法被调用时,才会对其进行重新布局
    • requestLayout()将当前View以及其所有父View标记为需要重新布局。并不意味兄弟View也会被重新测量和布局。只有当这个View的父View的布局参数(如宽度、高度、权重等)发生变化,或者父ViewonLayout()方法被重写改变子View的布局方式时,兄弟View才可能会被重新测量和布局
    • 仅调用forceLayout()requestLayout()并不会影响到其兄弟View的测量和布局,除非父View的布局参数或布局方式发生了变化

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/25337.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

可选链操作符

<span class"username">{{ username?.charAt(0) }}</span>这里我们使用了可选链操作符 &#xff1f;&#xff0c; 它的意思是当&#xff1f;前面的变量为空时&#xff0c;它不会继续往下执行&#xff0c;防止报错&#xff0c;如 null?.name&#xff0c…

手写kNN算法的实现-用余弦相似度来度量距离

设a为预测点&#xff0c;b为其中一个样本点&#xff0c;在向量空间里&#xff0c;它们的形成的夹角为θ&#xff0c;那么θ越小&#xff08;cosθ的值越接近1&#xff09;&#xff0c;就说明a点越接近b点。所以我们可以通过考察余弦相似度来预测a点的类型。 from collections i…

Linux Ext2/3/4文件系统

文章目录 前言一、Linux文件系统简介1.1 简介1.2 Linux File System Structure1.3 Directory Structure 二、Ext2/3/4文件系统2.1 Minix2.2 EXT2.3 EXT22.4 EXT32.5 EXT4 三、EXT Inode参考资料 前言 这篇文章介绍了Linux文件系统的一些基础知识&#xff1a;Linux 文件系统简介…

vs - vs2013中编译sqlite3.44.2

文章目录 vs - vs2013中编译sqlite3.44.2概述笔记工程输出归档END vs - vs2013中编译sqlite3.44.2 概述 以前在vs2019下编译了sqlite3.44.2, 好使。做了笔记(sqlite3.44.2的编译) 现在准备将手头的vs2019工程改为vs2013的&#xff0c;自然要将sqlite也编译为vs2013版本的。 按…

Javaweb02-XML概述

第一章 XML概述 1.XML基本概念 什么是xml&#xff1f; **a.**引入的原因&#xff1a;为了解决不同不同语言之间的数据传输的格式不同 **b.**概念&#xff1a;XML是一种可扩展标记语言&#xff0c;适用于不同数据之间的数据交换 **c.**XML文档&#xff1a;通过元素的嵌套&a…

[HNCTF 2022 WEEK4]flower plus

第一种花指令 第二种花指令 根据两种花指令特征&#xff0c;写出去花指令脚本 saddr0x401000 eaddr0x435000 for i in range(saddr,eaddr):if get_wide_dword(i)0x01740275:print(hex(i),hex(get_wide_dword(i)))patch_byte(i-5,0x90)patch_dword(i-4,0x90909090)patch_dw…

插卡式仪器模块:数字万用表模块(插卡式)

• 6 位数字表显示 • 24 位分辨率 • 250 KSPS 采样率 • 电源和数字 I/O 均采用隔离抗噪技术 • 电压、电流、电阻、电感、电容的高精度测量 • 二极管/三极管测试 通道122输入 阻抗 电压10 MΩHigh-Z, 10 MΩ电流10 Ω50 mΩ / 2 Ω / 2 KΩ输入范围电压 5 V0–60 V电流…

Ubuntu 配置动态链接器的搜索路径(/etc/ld.so.conf.d)

引言 为了使程序在运行时可以正常找到指定的lib库&#xff0c;防止出现卡死或闪退现象。 /etc/ld.so.conf.d 路径是程序运行时&#xff0c;系统查找共享库&#xff08;如 .so 文件&#xff09;的位置。 简要说明 如果你安装了一个第三方软件&#xff0c;它可能将其共享库放在…

【C语言】Leetcode-312 戳气球

文章目录 题目思路代码如下 题目 链接: Leetcode-312 戳气球 思路 我们观察戳气球的操作&#xff0c;发现这会导致两个气球从不相邻变成相邻&#xff0c;使得后续操作难以处理。于是我们倒过来看这些操作&#xff0c;将全过程看作是每次添加一个气球。 首先 我们需要创建一个…

【xilinx】使用vivado编译中methodology的相关介绍

Vivado Methodology 是 Xilinx 提供的一系列设计流程和最佳实践&#xff0c;旨在帮助工程师使用 Vivado Design Suite 工具链来开发高质量的 FPGA 和 SoC 应用。Vivado Methodology 涵盖了从设计开始到最终实现的整个流程&#xff0c;包括设计规划、实现、验证和优化等多个方面…

EF Core Model-First

使用C# .NET Core 3.1和EF Core Model-First方法生成数据库表的详细步骤&#xff0c;并介绍EF Core数据库迁移常用命令。 使用C# .NET Core 3.1和EF Core的Model-First方法生成数据库表 1. 安装EF Core包 首先&#xff0c;在你的项目中安装EF Core和相关的工具包。可以使用Nu…

后端Long类型参数前端接受精度丢失解决方案

背景 在项目中使用雪花Id作为主键Id使用&#xff0c;前端返回查询数据时在展示时出现精度丢失&#xff0c;如原Id为1797913405167583236进度丢失后为1797913405167583000 解决方案&#xff08;前后端&#xff09; 前端方案 前端解决精度问题很简单&#xff0c;通过字符串接…

58.CountdownLatch

用来进行线程同步协作,等待所有线程完成倒计时。 构造参数用来初始化等待计数值,await方法用来等待计数归零,countDown方法用来让计数减一。 CountdownLatch普通使用 @Slf4j public class CountdownLatchDemo {public static void main(String[] args) {CountDownLatch c…

Numpy基本操作之矩阵的检索与赋值

一 Numpy基本操作 创建矩阵 检索与赋值[y,x] 获取子数组[:,:] 二 Numpy检索与赋值 [y,x] [y,x,channel] import cv2import numpy as np#定义zeros矩阵 imgnp.zeros((480,640),np.uint8)print(img[100,100]) count0 while count<200:img[count,100,1]255countcount1cv2.i…

初学者使用sql时易犯的错误(持续更新)

DDL&#xff08;数据定义&#xff09; DROP 子句 用于删除指定的列名&#xff0c; ◼ 若选择RESTRICT &#xff0c;则删除的基本表不能被其他表的约 束所引用&#xff08;如 CHECK &#xff0c; FOREIGN KEY 等约束&#xff09;&#xff0c;不 能有视图&#xff0c;不能有触…

Python 冷门语法:深度探索与奇妙应用

Python 冷门语法&#xff1a;深度探索与奇妙应用 在Python的广阔天地中&#xff0c;除了那些广为人知的常用语法特性&#xff0c;还隐藏着一些冷门但极具魅力的语法。这些冷门语法虽然在日常编程中不常用&#xff0c;但它们的存在无疑为Python的多样性和灵活性增添了浓墨重彩的…

卷积的计算过程

卷积的计算过程 flyfish 包括手动计算&#xff0c;可视化使用torch.nn.Conv2d实现 示例 import torch import torch.nn as nn# 定义输入图像 input_image torch.tensor([[1, 2, 3, 0, 1],[0, 1, 2, 3, 4],[2, 3, 0, 1, 2],[1, 2, 3, 4, 0],[0, 1, 2, 3, 4] ], dtypetorch.f…

springboot 3 oauth2认证this.authorizationService.save(authorization)生成token报错异常

springboot 3 oauth2认证this.authorizationService.save(authorization)生成token报错异常&#xff0c;使用springboot版本3.3.0。 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId>&…

2024年政治经济学与社会科学国际会议(ICPESS 2024)

2024年政治经济学与社会科学国际会议 2024 International Conference on Political Economy and Social Sciences 会议简介 2024年政治经济学与社会科学国际会议是一个致力于探讨政治经济学与社会科学交叉领域前沿问题的国际盛会。本次会议汇聚了全球顶尖的专家学者、研究人员和…

DNS解析和bond网卡

DNS解析 ​ dns就是域名系统的简称,作用就是ip地址之间的映射关系 ​ 在互联网中,ip地址是通信的唯一标识,逻辑地址 ​ 访问网站,域名,ip地址不好记,域名朗朗上口,好记 ​ 域名解析的目的就是为了实现访问域名就等于ip地址 ​ 在访问域名的时候,通过dns解析,把域名映射的i…