如何获得我的 TextView 的精确位置(以像素为单位)?

How to get exact position in pixels of my TextView?

我有一部 2560 x 1440 像素的智能手机。现在我正在为我的 TextView:

使用这个函数
int[] locationOnScreen = new int[2];
txtAp.GetLocationInWindow(locationOnScreen);

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

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

I have a smartphone of 2560 x 1440 px... My TextView is pretty much in the middle of the screen, so supposedly at (1280,770).

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

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

重要说明GetLocationInWindow() returns 坐标 w.r.t 根视图,不是实际的 window。你应该 take the status bar height into consideration. And here is how you can calculate Height of status bar in Android.