如何获得TextView的精确位置?

时间:2017-11-15 16:33:44

标签: android android-layout position android-view

我有一台2560 x 1440像素的智能手机。现在我正在为TextView使用此功能:

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

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

怎么会这样?如果那不是那样做的话那么是什么?

1 个答案:

答案 0 :(得分:1)

  

我的智能手机为2560 x 1440像素...我的TextView几乎位于屏幕中间,所以据说是(1280,770)。

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

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

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