Android中状态栏的高度是多少?总是一样吗?

从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。

(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)


当前回答

240x320 - 20px 320x480 - 25px 480x800+ - 38px

其他回答

这个问题在… 状态栏的高度?

更新::

当前的方法:

好的,状态栏的高度取决于屏幕的大小,例如在一个设备中 对于240 X 320屏幕尺寸的设备,状态栏高度为20px,对于320 X 480屏幕尺寸的设备,状态栏高度为25px,对于480 X 800设备,状态栏高度必须为38px

所以我建议使用这个脚本来获取状态栏的高度

Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop = 
    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;

   Log.i("*** Elenasys :: ", "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight); 

(旧方法)获取onCreate()方法的状态栏的高度,使用这个方法:

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

如果你知道它的大小和高度

like

例如,在屏幕尺寸为320 X 480的设备中,状态栏高度为25px,对于屏幕尺寸为480 X 800的设备,状态栏高度必须为38px

然后你可以得到视图的宽度/屏幕大小,你可以使用if else语句来获得状态栏的高度

为了解决这个问题,我使用了一种组合方法。 这是必要的,因为在平板电脑上,当调用display.getHeight()时,系统栏已经减去了它的像素。 所以我首先检查系统栏是否存在,然后本克莱顿方法,这在手机上很有效。

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;
}

这也适用于引用链接

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

240x320 - 20px 320x480 - 25px 480x800+ - 38px