How to know when activity is laid out?

痞子三分冷 提交于 2019-12-07 07:42:25

I think you have to override onSizeChanged and call getWidth/Height from there. Be aware though that onSizeChanged might get called multiple times during your activity's lifecycle (Everytime the size of the view changes).

bigstones

From this post that I've found googling:

Assuming your view is visible, its size has been assigned by the time the activity's window receives focus. That is, when activity.onWindowFocusChanged is called with hasFocus = true.

The sequence is roughly:

view.onFinishInflate

view.onMeasure

view.onSizeChanged(width, height, 0, 0)

view.onLayout

activity.onWindowFocusChanged(true)

I tried and it worked.

EDIT

I've found that it's probably better using a ViewTreeObserver, see this answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!