【问题标题】:How to get exact position in pixels of my TextView?如何获得我的 TextView 的像素的确切位置?
【发布时间】:2018-04-28 23:44:21
【问题描述】:

我有一部 2560 x 1440 像素的智能手机。现在我将这个函数用于我的TextView

int[] locationOnScreen = new int[2];
txtAp.GetLocationInWindow(locationOnScreen);

它应该给我以像素为单位的总 x 和 y 坐标。 我的TextView 几乎在屏幕中间,所以应该在 (1280,770)。但该函数返回 [69, 1111]。

怎么可能?如果这不是这样做的方式,那是什么?

【问题讨论】:

    标签: android android-layout position android-view


    【解决方案1】:

    我有一部 2560 x 1440 像素的智能手机...我的 TextView 几乎在屏幕中间,所以应该是 (1280,770)。

    不正确,除非您谈论TextView 的中心。任何视图都在其矩形内呈现在屏幕上,因此其坐标被认为是 [left, top, right, bottom]。

    GetLocationInWindow() 方法返回视图的(左、上)坐标。所以 (x = 69, y = 1111) 看起来对位于屏幕中间的TextView 的左上角有意义。

    重要提示GetLocationInWindow() 返回根视图的坐标,而不是实际窗口。你应该take the status bar height into consideration。以下是计算 Height of status bar in Android 的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多