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

2024-06-10 03:28

本文主要是介绍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的布局参数或布局方式发生了变化

这篇关于View->View测量布局中requestLayout和forceLayout的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1047088

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

Before和BeforeClass的区别及说明

《Before和BeforeClass的区别及说明》:本文主要介绍Before和BeforeClass的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Before和BeforeClass的区别一个简单的例子当运行这个测试类时总结Before和Befor

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

Java 关键字transient与注解@Transient的区别用途解析

《Java关键字transient与注解@Transient的区别用途解析》在Java中,transient是一个关键字,用于声明一个字段不会被序列化,这篇文章给大家介绍了Java关键字transi... 在Java中,transient 是一个关键字,用于声明一个字段不会被序列化。当一个对象被序列化时,被

解读@ConfigurationProperties和@value的区别

《解读@ConfigurationProperties和@value的区别》:本文主要介绍@ConfigurationProperties和@value的区别及说明,具有很好的参考价值,希望对大家... 目录1. 功能对比2. 使用场景对比@ConfigurationProperties@Value3. 核

Spring Boot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)

《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2