android – 在应用程序生命周期的什么时候可以/应该使用布局测量?

关于这个问题有很多问题,但大多数都太专业了,不能回答我的问题.

我有一个谷歌地图,我告诉它适合它的相机到某些边界.也许不是很令人惊讶,我收到一个错误

java.lang.IllegalStateException:地图大小不应为0.很可能,地图视图尚未出现布局.

所以让我们将这个问题抽象到任何View.

这个“布局”事件何时实际发生?例如,onMeasure()没有出现在Activity Lifecycle中.何时可以安全地调用我需要布局的方法

解决方法

正如文档所说,你应该使用它
mapa.moveCamera(CameraUpdateFactory.newLatLngBounds(Builder.build(),this.getResources().getdisplayMetrics().widthPixels,this.getResources().getdisplayMetrics().heightPixels,50));

这是一项活动,相反

mapa.moveCamera(CameraUpdateFactory.newLatLngBounds(Builder.build(),50));

这对我有用.

文档

Note: Only use the simpler method newLatLngBounds(boundary,padding) to generate a CameraUpdate if it is going to be used to move the camera after the map has undergone layout. During layout,the API calculates the display boundaries of the map which are needed to correctly project the bounding Box. In comparison,you can use the CameraUpdate returned by the more complex method newLatLngBounds(boundary,width,height,padding) at any time,even before the map has undergone layout,because the API calculates the display boundaries from the arguments that you pass.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...