【问题标题】:Custom view onMeasure(...) width and height are 0 when AlertDialog is shown显示 AlertDialog 时自定义视图 onMeasure(...) 宽度和高度为 0
【发布时间】:2012-01-11 21:50:02
【问题描述】:

我的应用程序包含一个 MapActivity,它加载一个 XML 视图 (map_layout),其中包含一个 MapView 和一个扩展 ImageViewCustomView

我使用以下代码设置我的MapActivity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout);
    initialise();
}

然后在初始化时,我像往常一样使用 findViewById(...) 从 XML 文件中检索 MapViewCustomView

在我的CustomViewonMeasure(...) 函数中,我使用this.getWidth()this.getHeight() 来获取它的尺寸,这会返回正确的结果。

然后在我的initialise() 代码中,我决定检查 GPS 是否已启用(使用类似于此处第一个答案的代码:How do I find out if the GPS of an Android device is enabled),如果 GPS 已禁用,则显示 AlertDialog

现在的问题是我的CustomViewonMeasure(...) 函数不再正常工作,如果AlertDialog 显示,this.getWidth()this.getHeight() 都返回0。

为什么会这样?

提前感谢您的帮助,如果我的问题不清楚,我会在有机会时对其进行编辑以添加完整的代码。

【问题讨论】:

    标签: java android view bitmap android-alertdialog


    【解决方案1】:

    好的,出于某种原因,我需要在 onLayout() 而不是 onMeasure() 内获取高度和宽度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-17
      • 2015-08-01
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2012-08-27
      相关资源
      最近更新 更多