getWindowVisibleDisplayFrame() 在 Android 2.2、2.3 中给出不同的值(但不是 2.3.3)

发布于 2024-12-08 12:51:53 字数 574 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墟烟 2024-12-15 12:51:53

好吧,如果您阅读源代码中的注释,它承认这种方法有点损坏

    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;
    }

您将不得不忽略版本 < 的 outRect.top 值。 2.3.3

Welp, if you read the comments in the source, it admits that this method is kind of broken

    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;
    }

You will have to ignore the outRect.top value for versions < 2.3.3

独﹏钓一江月 2024-12-15 12:51:53

此处 是直接获取状态栏的链接,而不是计算它。它将避免 getWindowVisibleDisplayFrame 因平台和设备而不一致的任何问题。


它位于此处的回程机器上: https://web.archive.org/web/20130421032443/https://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android /

内容如下:

获取 Android 中状态栏的高度 2012 年 3 月 17 日

有时
在Android中,灵活的布局系统不够灵活,你
需要在代码中进行一些计算。在这些
计算时,您可能需要减去状态栏的大小。
Stackoverflow 给了你一些答案,但它们都依赖于事实
该状态栏会在您进行计算时显示。如果
您处于全屏模式,例如通过调用:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
这不起作用。

状态栏的高度包含在维度资源中
称为状态栏高度。它不属于公共资源,所以
你不能直接从你的代码访问它
android.R.dimen.status_bar_height。但是你可以计算它
运行时使用以下代码:

public int getStatusBarHeight() {   
  int result = 0;
  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");   
  if (resourceId > 0) {
    result = getResources().getDimensionPixelSize(resourceId);   
  }   
  return result; 
} 

您需要将此方法放入 ContextWrapper 中
类。

希望有帮助。

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:

Get the Height of the Status Bar in Android MAR 17TH, 2012

Sometimes
in Android, the flexible layout system is not flexible enough and you
need to make some computations inside your code. In these
computations, you may need to subtract the size of the status bar.
Stackoverflow gives you some answers, but they all rely on the fact
that te status bar is shown at the time you make your computation. If
you are in full screen mode, by having called for instance:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
It doesn’t work.

The height of the status bar is contained in a dimension resource
called status_bar_height. It’s not part of the public resources, so
you can’t access it directly from your code with
android.R.dimen.status_bar_height. You can however compute it at
runtime with the following code:

public int getStatusBarHeight() {   
  int result = 0;
  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");   
  if (resourceId > 0) {
    result = getResources().getDimensionPixelSize(resourceId);   
  }   
  return result; 
} 

You need to put this method in a ContextWrapper
class.

Hope it helps.

我不吻晚风 2024-12-15 12:51:53

有时,您需要在活动的 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.

故事↓在人 2024-12-15 12:51:53

所以,我这样做了:

    Rect rectangle = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
    if(android.os.Build.VERSION.SDK_INT < 9  /* android.os.Build.VERSION_CODES.GINGERBREAD */ ) {
        // http://stackoverflow.com/questions/7659652/getwindowvisibledisplayframe-gives-different-values-in-android-2-2-2-3-but-no/7660204#7660204
        rectangle.top = 0;
    }

So, I did this:

    Rect rectangle = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
    if(android.os.Build.VERSION.SDK_INT < 9  /* android.os.Build.VERSION_CODES.GINGERBREAD */ ) {
        // http://stackoverflow.com/questions/7659652/getwindowvisibledisplayframe-gives-different-values-in-android-2-2-2-3-but-no/7660204#7660204
        rectangle.top = 0;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文