gpt4 book ai didi

android - getLocationInWindow 和 getLocationOnScreen 之间的混淆

转载 作者:太空狗 更新时间:2023-10-29 13:27:13 44 4
gpt4 key购买 nike

据我了解,getLocationOnScreen 返回的位置在 Y 轴上从屏幕角的最左上角起增加了状态栏(或操作栏或标题栏?)的高度。

getLocationInWindow 返回 Activity 根内容 View 左上角的位置。

现在,一切似乎都说得通了。但是当我尝试使用 getLocationOnScreengetLocationInWindow 获取位置时,它们都返回相同的按钮位置,并增加了状态栏的高度。对于 getLocationOnScreen 它似乎是正确的但对于 getLocationInWindow 它似乎是错误的。

有什么我想念的吗?或者它只是 buggy ?我在 API-4 和 API-14 中对此进行了测试。

最佳答案

尽管这个问题之前已经回答过,但我认为仍然值得努力让它更清楚。

如果您查看 getLocationOnScreen 和 getLocationInWindow 的代码:

public void getLocationOnScreen(int[] outLocation) {
// It calls the getLocationInWindow
getLocationInWindow(outLocation);

// and adjust accordingly in case the window is not the top-level window
final AttachInfo info = mAttachInfo;
if (info != null) {
outLocation[0] += info.mWindowLeft; // refer image below
outLocation[1] += info.mWindowTop; // refer image below
}
}

public void getLocationInWindow(int[] outLocation) {
// do my calculation here
// by traversing views contained in this window (in a single tree of views)
...
}

这在下图中有进一步解释,其中蓝色表示屏幕,红色表示窗口:

enter image description here

重要的是要注意,窗口可以是您的顶级窗口(覆盖整个屏幕),也可以是其他自定义窗口,例如对话框。

那么,回到你的问题:

getLocationOnScreen returns location with added height of status bar

没错。您手机的屏幕包括状态栏的 View

getLocationOnScreen and getLocationInWindow, they both return same location of a button with added height of status bar

那是因为您正在使用的窗口是覆盖整个手机屏幕的顶级窗口。

关于android - getLocationInWindow 和 getLocationOnScreen 之间的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20138099/

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