当前位置: 移动技术网 > IT编程>开发语言>.net > Android从源码分析handler.post(runnable),view.post(runnable),runOnUiThread(runnable)执行时机

Android从源码分析handler.post(runnable),view.post(runnable),runOnUiThread(runnable)执行时机

2020年08月01日  | 移动技术网IT编程  | 我要评论
参考: https://blog.csdn.net/fengshenlangzi/article/details/51727264?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2

参考: https://blog.csdn.net/fengshenlangzi/article/details/51727264?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

view/activity的生命周期关系

在MyActivity中布局添加一个CustomView,然后在各个回调中添加log,会有如下调用顺序:

E/MyActivity: onCreate: 
E/CustomView: onFinishInflate: 
E/MyActivity: onResume: 
E/MyActivity: onAttachedToWindow: 
E/CustomView: onAttachedToWindow: 
E/CustomView: onMeasure: 
E/CustomView: onMeasure: 
E/CustomView: onMeasure: 
E/CustomView: onSizeChanged: 
E/CustomView: onLayout: 
E/CustomView: onDraw: 
E/MainActivity: onWindowFocusChanged: true
E/CustomView: onWindowFocusChanged: true

//接下来退出Activity
E/MainActivity: onWindowFocusChanged: false
E/CustomView: onWindowFocusChanged: false
E/MyActivity: onDestroy: 
E/CustomView: onDetachedFromWindow: 
E/MyActivity: onDetachedFromWindow: 

因此如果是在 onCreate或者是onResume 里面获取View的 宽高,得到的都是0.

可以用View.post(Runnable)来延后 执行

本文地址:https://blog.csdn.net/GYBIN02/article/details/108129496

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网