getWindowVisibleDisplayFrame() gives different values in Android 2.2, 2.3 (but not 2.3.3)

前端 未结 4 1240
执笔经年
执笔经年 2021-01-06 03:11

I\'ve got an Activity which uses

getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);

to determine the useable

相关标签:
4条回答
  • 2021-01-06 03:20

    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.

    0 讨论(0)
  • 2021-01-06 03:21

    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.

    0 讨论(0)
  • 2021-01-06 03:23

    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

    0 讨论(0)
  • 2021-01-06 03:36

    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;
        }
    
    0 讨论(0)
提交回复
热议问题