gpt4 book ai didi

android - 获取 View 相对于根布局的坐标

转载 作者:IT老高 更新时间:2023-10-28 12:59:10 27 4
gpt4 key购买 nike

我可以在 Android 中获取相对于我的 Activity 的根布局的 View 的 x 和 y 位置吗?

最佳答案

Android API 已经提供了一种方法来实现这一点。试试这个:

Rect offsetViewBounds = new Rect();
//returns the visible bounds
childView.getDrawingRect(offsetViewBounds);
// calculates the relative coordinates to the parent
parentViewGroup.offsetDescendantRectToMyCoords(childView, offsetViewBounds);

int relativeTop = offsetViewBounds.top;
int relativeLeft = offsetViewBounds.left;

这里是 doc

关于android - 获取 View 相对于根布局的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3619693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com