要获取子视图在屏幕上的位置,可以使用子视图的getLocationInWindow()函数。
示例代码如下:
int[] location = new int[2];
childView.getLocationInWindow(location);
int x = location[0]; // 子视图在屏幕上的x坐标
int y = location[1]; // 子视图在屏幕上的y坐标
这样可以获取到子视图在屏幕上的位置。请注意,这里使用的是getLocationInWindow()函数,而不是getLocationOnScreen()函数。这两个函数的区别在于,getLocationInWindow()返回的是子视图在窗口中的位置,而getLocationOnScreen()返回的是子视图在屏幕上的位置,包括状态栏的高度。