在此情况下,建议使用getLocationInWindow方法获取视图在窗口内的坐标。以下是示例代码:
// 获取视图在窗口内的坐标
int[] location = new int[2];
view.getLocationInWindow(location);
int x = location[0];
int y = location[1];
// 注意:如果需要在屏幕上获取坐标,需要考虑状态栏和导航栏的高度
Resources resources = context.getResources();
int statusBarHeight = resources.getDimensionPixelSize(resources.getIdentifier("status_bar_height", "dimen", "android"));
int navigationBarHeight = resources.getDimensionPixelSize(resources.getIdentifier("navigation_bar_height", "dimen", "android"));
int s
cr
eenX = x;
int s
cr
eenY = y - statusBarHeight;
if (navigationBarHeight > 0) {
s
cr
eenY -= navigationBarHeight;