如何知道什么时候活动奠定了?什么时候

2023-09-06 23:25:39 作者:Agoni

我已经得到了我想要把一些标记之上一个图形页面。我会从我开始的活动web服务检索这些,所以我需要知道当前视口的最小和最大纬度/经度对。我打电话

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

但他们都返回0,而活动正在启动。我试图把它onAttachedToWindow,onResume,onPostCreate,onPostResume,在onStart等,但都无济于事。我怎么能知道该活动已完成铺设了各方面的意见,并准备给我正确的高度和宽度测量?

But they both return 0 while the activity is starting. 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?

推荐答案

我认为你必须重写 onSizeChanged 并从那里调用的getWidth /高度。要知道,虽然你的活动的生命周期(的看法改变每次的大小)期间onSizeChanged可能会被多次调用。

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