【问题标题】:Get absolute coordinates of view inside popup window获取弹出窗口内视图的绝对坐标
【发布时间】:2017-11-14 20:55:28
【问题描述】:

我有一个可拖动的弹出窗口片段。我需要在 onSaveInstanceState() 中获取该片段的 x 和 y 坐标。我通过使用getView().getWidth()getView().getHeight() 获得了片段的宽度和高度。但如果我使用getView().getX()getView().getY() 给出0.0。有没有机会得到坐标?

【问题讨论】:

    标签: android android-layout android-view android-popupwindow android-window


    【解决方案1】:

    使用View#getLocationOnScreen(int[]) API:

    计算此视图在屏幕上的坐标。参数必须是两个整数的数组。方法返回后,数组按顺序包含 x 和 y 位置。

    int[] location = new int[2]; getView().getLocationOnScreen(location); // now location[0] contains x position // and location[1] contains y position

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多