可以使用View.getLocationOnScreen()方法获取childView在屏幕上的坐标,然后减去父控件在屏幕上的坐标(也可以使用View.getLocationInWindow()方法获取父控件在屏幕上的坐标),就可以得到childView在父控件里面的坐标。具体代码如下:

int[] childLocation = new int[2];
childView.getLocationOnScreen(childLocation);

int[] parentLocation = new int[2];
parentView.getLocationOnScreen(parentLocation);

int x = childLocation[0] - parentLocation[0];
int y = childLocation[1] - parentLocation[1];
android获取childView在父控件里面的坐标

原文地址: https://www.cveoy.top/t/topic/g13M 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录