Reliably get height of status bar to solve KitKat translucent navigation issue

后端 未结 4 1331
轻奢々
轻奢々 2020-12-14 01:45

I am experimenting with the new Android 4.4 translucent navigation bars and would like to set the navigation bar as translucent using the FLAG_TRANSLUCENT_NAVIGATION

4条回答
  •  悲哀的现实
    2020-12-14 01:50

    The accepted answer always returns the status bar height (and in a somewhat hacky way). But some activities may actually be fullscreen, and this method doesn't differentiate between them.

    This method works perfectly for me to find the status bar height relative to the current activity (place it in your Activity class, and use it once layout has finished):

    public int getStatusBarHeight() {
        Rect displayRect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(displayRect);
        return displayRect.top;
    }
    

    Note you could also just use displayRect directly in case you have other "window decorations" at the bottom or potentially even the sides of the screen.

提交回复
热议问题