开发者

How to know when activity is laid out?

I've got a mapview that I want to put some markers on top of. I'll retrieve these from a webservice when I start the activity, so I need to know the minimum and maximum lat/lng pairs for the current viewport. I'm calling

mMapView.getWidth()
mMapView.getHeight()

But they both return 0 while the activity is starting.开发者_Go百科 I tried putting it in onAttachedToWindow, onResume, onPostCreate, onPostResume, onStart and so on, but to no avail. How can I know that the activity has finished laying out all the views and is ready to give me the correct height and width measurements?


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜