How to get screen resolution in Android Honeycomb?

2019-02-01 01:53发布

I want to get real resolution of screen on Android Honeycomb.

Here's my code

Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();

My device is Asus Transformer TF101 with size are 1280x800.

But above code make w = 1280 and h = 752 (That i want is 800 not 752).

I know h < 800 because it's subtracted for status bar.

Have any way to get real height of screen?

Many thanks!

10条回答
Fickle 薄情
2楼-- · 2019-02-01 02:30

You can extend a layout which will be your top layout (which will be set to fill_parent width and height) and override the onSizeChange(int width, int height, int oldwidth, int oldheight). The width and height is the real width and height of your display.

查看更多
甜甜的少女心
3楼-- · 2019-02-01 02:34

Use the DisplayMetrics structure, describing general information about the display, such as its size, density, and font scaling.

The code used to get the display's height is as follows:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Log.d("log", "OOO " + metrics.heightPixels);

It's supposed to return the absolute height of the display, in pixels.

查看更多
祖国的老花朵
4楼-- · 2019-02-01 02:37

I have a Xoom in hand, and used getWindowManager().getDefaultDisplay() to retrieve the dimension, it returns me 1280 x 800 which is the full screen size. I wonder how to get the resolution minus navigation bar with API before version 3.0.

查看更多
看我几分像从前
5楼-- · 2019-02-01 02:41

This is another viable solution for measuring the screen size (API 11+):

public static ABDimension calculateScreenDimensions(Activity activityGet)
{
    ABDimension abdReturn = new ABDimension();

    try
    {
        Rect rectGet = new Rect();
        Window winGet = null;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        int nStatusBarHeight = 0;
        int nContentViewTop = 0;
        int nTitleBarHeight = 0;
        int nScreenHeight = 0;

        if((activityGet != null) && (rectGet != null) && (displayMetrics != null))
        {
            winGet = activityGet.getWindow();
            if(winGet != null)
            {
                winGet.getDecorView().getWindowVisibleDisplayFrame(rectGet);
                nStatusBarHeight = rectGet.top;
                nContentViewTop = winGet.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                nTitleBarHeight = nContentViewTop - nStatusBarHeight;

                activityGet.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                int screenHeight = displayMetrics.heightPixels;
                abdReturn.nWidth = displayMetrics.widthPixels;
                abdReturn.nHeight = screenHeight - (nTitleBarHeight + nStatusBarHeight);
            }
        }
    }
    catch(Exception e)
    {
        appContext.showMessage(false,"Error","[calculateScreenDimensions]: "+e.toString());
    }

    return abdReturn;
}

Where the ABDimension class contains six integers labeled nWidth, nHeight, nMarginLeft, nMarginTop, nMarginRight and nMarginBottom. This version accounts for common Android decor components like the TitleBar/StatusBar.

查看更多
登录 后发表回答