Getting the actual screen height (android)

前端 未结 4 1005
北海茫月
北海茫月 2021-01-17 10:36

I want to get the actual screen height of the device that runs my app. To achieve this i try the following:

DisplayMetrics metrics = new DisplayMetrics();
get         


        
4条回答
  •  礼貌的吻别
    2021-01-17 10:55

    see this answer

    if your API level > 13 try this if you are in activity

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    

    If you're not in an Activity you can get the default Display via WINDOW_SERVICE:

    WindowManager wm = (WindowManager)    context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    

    getWidth and getHeight is for API level 13 or less

    UPDATE

    For API 17 and higher method Display.getRealSize() returns full size of screen, as mentioned in documentation:

    Gets the real size of the display without subtracting any window decor or applying any compatibility scale factors.

    The size is adjusted based on the current rotation of the display.

    The real size may be smaller than the physical size of the screen when the window manager is emulating a smaller display (using adb shell am display-size).

提交回复
热议问题