Get the screen height in Android

前端 未结 9 1268
忘了有多久
忘了有多久 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:18

    I haven't tried different API's but I'm using these values to figure out whether or not the onscreen keyboard is up. Maybe you can try this and something comparable for width to get what you need?

    Rect r = new Rect();
    view.getWindowVisibleDisplayFrame(r);
    int
        screenHeight = view.getRootView().getHeight(), // height of the entire screen
        appHeight = r.bottom - r.top, // height of the entire app
        statusBarHeight = r.top, // height of the statusbar
        pageHeight = view.getHeight(), // height of the app without title
        appTitleHeight = appHeight - pageHeight, // height of the only the app title
        topHeight = statusBarHeight + appTitleHeight;
    

提交回复
热议问题