如何获得我的TextView像素的确切位置? [英] How to get exact position in pixels of my TextView?
问题描述
我有2560 x 1440像素的智能手机.现在,我在TextView
中使用此功能:
I have a smartphone of 2560 x 1440 px. Now I am using this function for my TextView
:
int[] locationOnScreen = new int[2];
txtAp.GetLocationInWindow(locationOnScreen);
应该给我x和y的总坐标(以像素为单位).
我的TextView
几乎位于屏幕中间,所以应该位于
(1280,770).但是该函数返回[69,1111].
It is supposed to give me the total x and y coordinates in pixels.
My TextView
is pretty much in the middle of the screen, so supposedly at
(1280,770). But the function returns [69, 1111].
那怎么可能?如果那不是这样做的方式,那是什么?
How can that be? If that is not the way of doing that, then what is?
推荐答案
我有2560 x 1440 px的智能手机...我的
TextView
几乎在屏幕中间,所以应该是(1280,770).
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
的中心.屏幕上的任何视图都呈现在其矩形内,因此其坐标被认为是[左,上,右,下].
Not correct, unless you talk about the center of the TextView
. Any view is rendered on screen within its rectangle, so its coordinates are considered to be of [left, top, right, bottom].
GetLocationInWindow()
方法返回视图的(左,上)坐标.因此(x = 69,y = 1111)对于位于屏幕中间的TextView
的左上角看起来很有意义.
The GetLocationInWindow()
method returns (left, top) coordinates of a view. So (x = 69, y = 1111) looks to be meaningful for the left top corner of your TextView
located in the middle of screen.
重要说明:GetLocationInWindow()
返回不带根视图的坐标,而不是实际窗口.您应该考虑状态栏的高度.这就是您计算 Android中状态栏高度的方式.
Important note: GetLocationInWindow()
returns the coordinates w.r.t the root view, not actual window. You should take the status bar height into consideration. And here is how you can calculate Height of status bar in Android.
这篇关于如何获得我的TextView像素的确切位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!