Get the screen height in Android

前端 未结 9 1190
忘了有多久
忘了有多久 2021-02-05 09:57

How can I get the available height of the screen in Android? I need to the height minus the status bar / menu bar or any other decorations that might be on screen and I need it

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 10:14

    For Display

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;
    

    For Status Bar

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

提交回复
热议问题