【Android】在渲染生效前提前测量View大小

问题

Android的UI机制是基于MessageHandler的,View并不会在添加后就会被立刻测量渲染出来

但有时我们需要动态去计算布局,希望在添加View时,就能知道这个View的高度

方法

此时,我们可以手动去调用measure方法,来测量View大小

measure方法需要提供两个spec,我们根据实际情况自己去创建

代码
kotlin 复制代码
view.measure(
  MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
  MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
)
// optional
view.layout(0, 0, view.measuredWidth, view.measuredHeight)
view.requestLayout()
view.invalidate()
相关推荐
Code Man9 分钟前
Windows 下使用 Appium
android·windows·appium
码农coding19 分钟前
android 12 中的VSYNC接收
android
GitLqr31 分钟前
Flutter 3.44 性能飞跃:深度解析 Android Platform View 的 HCPP 新特性
android·flutter·性能优化
码农coding35 分钟前
android 12 SurfaceFlinger中的CompositionEngine
android
Mem0rin2 小时前
[MySQL] 聚合函数、分组查询、连接查询
android·mysql
码龙-DragonCoding2 小时前
一键批量提取音频、提取视频
android·音视频·提取
祉猷并茂,雯华若锦3 小时前
Win下完美解决Allure报错,生成Web自动化测试报告
android·python·selenium·自动化
IT小盘4 小时前
08-FastAPI加MySQL实现AI对话记录持久化
android·mysql·fastapi
alice--小文子4 小时前
安卓(Android)- 怎么在adb中通过真机操作日志相关
android·adb
清泓y4 小时前
Android内存管理与性能优化面试真题
android·面试·性能优化