Android-面试官:View-post()-为什么能够获取到-View-的宽高-?

本文探讨了在Android中何时可以获取到View的宽高,指出View的测量发生在onResume流程之后。通过分析代码流程,揭示View.post()能够在View被测量后执行,因此能在其中获取到正确的宽高。文章深入讲解了从应用启动到View绘制的全过程,以及ViewRootImpl和Handler在其中的作用。
摘要由CSDN通过智能技术生成

// 在 onResume() 回调中获取宽高
Log.e(“measure”,“measure in onResume: width= w i n d o w . d e c o r V i e w . w i d t h , h e i g h t = {window.decorView.width}, height= window.decorView.width,height={window.decorView.height}”)
}
}

大多数人都能直截了当的给出答案:

E/measure: measure in onCreate: width=0, height=0
E/measure: measure in onResume: width=0, height=0
E/measure: measure in View.post: width=1080, height=2340

onCreate()onResume() 中是无法获取到宽高的,而 View.post() 回调中可以。从日志打印顺序可以看出来&#x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值