getWindowVisibleDisplayFrame() 在 Android 2.2、2.3 中给出不同的值(但不是 2.3.3)
我有一个 Activity
用于
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
确定可用的屏幕空间并决定放置图像的位置。
单击硬件“后退”按钮离开 Activity
后返回 Activity,矩形值为
(0,0,800,480)
但是,单击硬件“home”按钮离开 后返回 Activity Activity
,矩形值会
(0,38,800,480)
影响显示和图像位置。
如何确保在通话时获得一致的值
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
无论我如何离开应用程序,
?更新:感谢@Reno 帮助测试;它似乎依赖于 Android 版本而不是设备。
I've got an Activity
which uses
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
to determine the useable screen space and decide where to place images.
Returning to the Activity after I click the hardware "back" button to leave the Activity
, the rectangle values are
(0,0,800,480)
However, returning to the Activity after I click the hardware "home" button to leave the Activity
, the rectangle values are
(0,38,800,480)
which throws off the display and the image placement.
How can I ensure I get a consistent values when calling
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
no matter how I left the app?
UPDATE: Thanks to @Reno for helping test; it seems to be dependent on Android version than the device.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,如果您阅读源代码中的注释,它承认这种方法有点损坏
您将不得不忽略版本 < 的 outRect.top 值。 2.3.3
Welp, if you read the comments in the source, it admits that this method is kind of broken
You will have to ignore the outRect.top value for versions < 2.3.3
此处 是直接获取状态栏的链接,而不是计算它。它将避免 getWindowVisibleDisplayFrame 因平台和设备而不一致的任何问题。
它位于此处的回程机器上: https://web.archive.org/web/20130421032443/https://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android /
内容如下:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
这不起作用。
here is a link to get status bar directly instead of calculate it. It will avoid any issue of getWindowVisibleDisplayFrame which is really inconsistent through platforms and devices.
It's on wayback machine here: https://web.archive.org/web/20130421032443/https://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/
And here is the content:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
It doesn’t work.
有时,您需要在活动的 onCreate 中了解布局可用空间的精确尺寸。
经过一番思考,我想出了这样的方法: https://stackoverflow.com/a/16691531/2202013
它不使用 getWindowVisibleDisplayFrame,并且有望面向未来。
There are times when you need to know the precise dimensions of the available space for a layout when in an activity's onCreate.
After some thought I worked out this way of doing it: https://stackoverflow.com/a/16691531/2202013
It does not use getWindowVisibleDisplayFrame and is hopefully future proof.
所以,我这样做了:
So, I did this: