View的学习(一)

这篇博客介绍了Android中View的三个关键方法:requestLayout()用于触发重新布局,使视图在测量和布局阶段重新计算;invalidate()则强制视图重绘,执行onDraw();而setMeasuredDimension()用于在onMeasure()中设置视图的测量宽度和高度,确保正确的尺寸。这些方法对于自定义视图的开发至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本章主要讲述View的几个常用方法,对于后期自定义控件非常有用。

1、requestLayout()

API文档中有这样的介绍:

 Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in measure(int, int)......The second pass happens in layout(int, int, int, int)......

To intiate a layout, call requestLayout(). This method is typically called by a view on itself when it believes that is can no longer fit within its current bounds

因此,requestLayout()调用时,控件会重新执行 onMesure()onLayout()

2、invalidate()

文档原话:To force a view to draw, call invalidate().

因此,invalidate()调用时,会执行onDraw()方法。

3、setMeasuredDimension (int measuredWidth, int measuredHeight)

文档说明:This method must be called by onMeasure(int, int) to store the measured width and measured height. Failing to do so will trigger an exception at measurement time.

该方法用于设置View的宽高,是在onMessure()调中执行(也就是说依赖于onMessure())。因此,对于View的宽高更改,我们可以重写onMessure(),并用setMeasuredDimension ()进行设置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值