我有一个 Activity 使用
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
确定可用的屏幕空间并决定放置图像的位置。
点击硬件“后退”按钮离开Activity后返回Activity,矩形值为
(0,0,800,480)
但是,在我点击硬件“home”按钮离开Activity后返回到Activity,矩形值为
(0,38,800,480)
这会影响显示和图像放置。
如何确保在调用时获得一致的值
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
无论我如何离开应用程序?
更新:感谢@Reno 帮助测试;它似乎取决于 Android 版本而不是设备。
最佳答案
好吧,如果你阅读了源代码中的评论,它承认这个方法有点坏了
public void getWindowVisibleDisplayFrame(Rect outRect) {
if (mAttachInfo != null) {
try {
mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
} catch (RemoteException e) {
return;
}
// XXX This is really broken, and probably all needs to be done
// in the window manager, and we need to know more about whether
// we want the area behind or in front of the IME.
final Rect insets = mAttachInfo.mVisibleInsets;
outRect.left += insets.left;
outRect.top += insets.top;
outRect.right -= insets.right;
outRect.bottom -= insets.bottom;
return;
}
您将不得不忽略版本 < 2.3.3="">
的 outRect.top 值关于android - getWindowVisibleDisplayFrame() 在 Android 2.2、2.3(但不是 2.3.3)中给出不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7659652/