Height of status bar in Android

2018-12-31 15:29发布

What's the height of the status bar in Android? Is it always the same?

From my measurements it seems that it's 25dp, but I'm not sure if it has the same height on all platforms.

(I want to know this to properly implement a fade transition from an activity that doesn't have status bar to one that does)

20条回答
后来的你喜欢了谁
2楼-- · 2018-12-31 15:44

240x320 - 20px

320x480 - 25px

480x800+ - 38px

查看更多
爱死公子算了
3楼-- · 2018-12-31 15:44

To solve this, I used a combination approach. This is necessary as on tablets the system bar already subtracts it's pixels when display.getHeight() is called. So I first check if a system bar is present, and then Ben Claytons approach, which works fine on phones.

public int getStatusBarHeight() {
    int statusBarHeight = 0;

    if (!hasOnScreenSystemBar()) {
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
    }

    return statusBarHeight;
}

private boolean hasOnScreenSystemBar() {
    Display display = getWindowManager().getDefaultDisplay();
    int rawDisplayHeight = 0;
    try {
        Method getRawHeight = Display.class.getMethod("getRawHeight");
        rawDisplayHeight = (Integer) getRawHeight.invoke(display);
    } catch (Exception ex) {
    }

    int UIRequestedHeight = display.getHeight();

    return rawDisplayHeight - UIRequestedHeight > 0;
}
查看更多
公子世无双
4楼-- · 2018-12-31 15:45

The default height used to be 25dp. With Android Marshmallow (API 23) the height was reduced to 24dp.

查看更多
几人难应
5楼-- · 2018-12-31 15:45

Try this:

    Rect rect = new Rect();
    Window win = this.getWindow();
    win.getDecorView().getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rect.top;
    int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
    int titleBarHeight = contentViewTop - statusBarHeight;
    Log.d("ID-ANDROID-CONTENT", "titleBarHeight = " + titleBarHeight );

it didn't work for me in the onCreate method for the activity, but did when I put it in an onClickListener and gave me a measurement of 25

查看更多
宁负流年不负卿
6楼-- · 2018-12-31 15:47

According to Google design guidelines; height of status bar is 24 dp.

If you want get status bar height in pixels you can use below method:

private static int statusBarHeight(android.content.res.Resources res) {
    return (int) (24 * res.getDisplayMetrics().density);
}

which can be called from activity with:

statusBarHeight(getResources());
查看更多
残风、尘缘若梦
7楼-- · 2018-12-31 15:48

Hardcoding the size or using reflection to get the value of status_bar_height is considered bad practice. Chris Banes talked about this in at the Droidcon New York. The recommended way of getting the status bar size is via the OnApplyWindowInsetsListener:

myView.setOnApplyWindowInsetsListener { view, insets -> {
  val statusBarSize = insets.systemWindowInsetTop
  return insets
}

This was added in API 20 and is also backported via ViewAppCompat.

查看更多
登录 后发表回答