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

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

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


当前回答

是的,当我尝试使用视图时,它提供了25px的结果。 以下是整个代码:

public class SpinActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout lySpin = new LinearLayout(this);
        lySpin.setOrientation(LinearLayout.VERTICAL);       
        lySpin.post(new Runnable()
        {
            public void run()
            {
                Rect rect = new Rect();
                Window window = getWindow();
                window.getDecorView().getWindowVisibleDisplayFrame(rect);
                int statusBarHeight = rect.top;
                int contentViewTop = 
                    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                int titleBarHeight = contentViewTop - statusBarHeight;
                System.out.println("TitleBarHeight: " + titleBarHeight 
                    + ", StatusBarHeight: " + statusBarHeight);
            }
        }
    }
}

其他回答

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

在我用来获取状态栏高度的所有代码示例中,唯一一个在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/

试试这个:

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

它没有为我工作在onCreate方法的活动,但当我把它放在onClickListener,并给我一个25的测量

这也适用于引用链接

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

Kotlin版本,结合了两个最佳解决方案

fun getStatusBarHeight(): Int {
    val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
    return if (resourceId > 0) resources.getDimensionPixelSize(resourceId)
    else Rect().apply { window.decorView.getWindowVisibleDisplayFrame(this) }.top
}

如果存在,则接受status_bar_height值 如果status_bar_height不存在,则从Window decor计算状态栏高度