Android 获取子视图在父视图中的坐标 - 详细代码示例
{"title":"Android 获取子视图在父视图中的坐标 - 详细代码示例","description":"本文将详细介绍如何使用 Android 的 View.getLocationOnScreen() 方法获取子视图在屏幕上的坐标,并通过减去父视图的坐标获得子视图在父视图中的位置。","keywords":"Android, 子视图, 父视图, 坐标, getLocationOnScreen, getLocationInWindow, 代码示例","content":"可以使用 View.getLocationOnScreen() 方法获取 childView 在屏幕上的坐标,然后减去父控件在屏幕上的坐标(也可以使用 View.getLocationInWindow() 方法获取父控件在屏幕上的坐标),就可以得到 childView 在父控件里面的坐标。具体代码如下:\n\n\nint[] childLocation = new int[2];\nchildView.getLocationOnScreen(childLocation);\n\nint[] parentLocation = new int[2];\nparentView.getLocationOnScreen(parentLocation);\n\nint x = childLocation[0] - parentLocation[0];\nint y = childLocation[1] - parentLocation[1];\n\n
原文地址: https://www.cveoy.top/t/topic/oz5O 著作权归作者所有。请勿转载和采集!