gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:31 25 4
gpt4 key购买 nike

关于这个问题有很多问题,但大多数都过于专业,无法回答我的问题。

我有一个 GoogleMap,我告诉它使其相机适合特定范围。也许并不奇怪,我得到一个错误:

java.lang.IllegalStateException: map 大小不应为 0。很可能, map View 尚未发生布局。

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

这个“布局”事件实际发生在什么时候? onMeasure() 没有出现在 Activity Lifecycle 中, 例如。什么时候调用我的需要布局的方法是安全的?

最佳答案

正如文档所说,你应该使用这个

mapa.moveCamera(CameraUpdateFactory.newLatLngBounds(Builder.build(), 
this.getResources().getDisplayMetrics().widthPixels,
this.getResources().getDisplayMetrics().heightPixels,
50));

这是一项 Activity ,而不是

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 - 在应用程序生命周期的什么时候可以/应该使用布局测量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14428766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com