Android中状态栏的高度是多少?它总是一样的吗?
根据我的测量,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这一点,以便正确地实现从没有状态栏的活动到有状态栏的活动的淡出转换)
发布于 2010-08-05 06:02:44
这个问题之前已经回答过了.Height of statusbar?
更新::
当前方法:
好的,状态栏的高度取决于屏幕尺寸,例如,在屏幕尺寸为240×320的设备中,状态栏高度为20px,对于屏幕尺寸为320×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); (旧方法)要获取Activity的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;
} 发布于 2013-01-08 18:35:39
在我用来获取状态栏高度的所有代码示例中,唯一看起来在Activity的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;
}显然,状态栏的实际高度是作为Android资源保存的。上面的代码可以添加到ContextWrapper类(例如Activity)中。
可在http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/找到
发布于 2017-11-06 03:19:53
硬编码大小或使用反射来获取status_bar_height的值被认为是不好的做法。克里斯·班斯talked about this in at the Droidcon New York报道。获取状态栏大小的推荐方法是通过OnApplyWindowInsetsListener
myView.setOnApplyWindowInsetsListener { view, insets -> {
  val statusBarSize = insets.systemWindowInsetTop
  return insets
}这是在API20中添加的,也是通过ViewAppCompat向后移植的。
https://stackoverflow.com/questions/3407256
复制相似问题