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

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

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


当前回答

切换全屏解决方案:

这个解决方案可能看起来像一个变通方法,但它实际上解释了你的应用程序是否全屏(也就是隐藏状态栏):

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
int barheight = size.y - findViewById(R.id.rootView).getHeight();

这样,如果你的应用程序目前是全屏的,barheight将等于0。

就我个人而言,我不得不使用这个来纠正绝对TouchEvent坐标,以解释状态栏:

@Override
public boolean onTouch(View view,MotionEvent event) {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point(); display.getSize(size);
    int YCoord = (int)event.getRawY() - size.y + rootView.getHeight());
}

那会得到绝对y坐标无论应用是否全屏。

享受

其他回答

根据材料指南;状态栏高度为24dp。

如果你想获得状态栏的高度,你可以使用下面的方法:

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

可以从activity中调用:

statusBarHeight(getResources());

这个问题最近与我有关,因为我的Pixel 3XL上的缺口。我真的很喜欢android开发者的解决方案,但我希望能够随心所欲地获得状态栏的高度,因为这对于我需要播放的全屏动画是特别必要的。下面的函数启用了一个可靠的查询:

private val DEFAULT_INSET = 96
fun getInsets(view: View?): Int {
     var inset = DEFAULT_INSET
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//Safe because only P supports notches
          inset = view?.rootWindowInsets?.stableInsetTop ?: DEFAULT_INSET
     }
     return inset
}

fun blurView(rootView: View?, a: SpacesActivity?) {
    val screenBitmap = getBitmapFromView(rootView!!)
    val heightDifference = getInsets(rootView)
    val croppedMap = Bitmap.createBitmap(
                    screenBitmap, 0, heightDifference,
                    screenBitmap.width,
                    screenBitmap.height - heightDifference)
    val blurredScreen = blurBitmap(croppedMap)
    if (blurredScreen != null) {
         val myDrawable = BitmapDrawable(a!!.resources, blurredScreen)
         a.errorHudFrameLayout?.background = myDrawable
         a.appBarLayout?.visibility = View.INVISIBLE
   }
}

然后在活动课上

fun blurView() {
    this.runOnUiThread {
        Helper.blurView(this)
    }
}

您当然希望将活动的弱引用传递给静态Helper类方法参数,但为了简洁起见,我在本例中没有这样做。由于同样的原因,blurbitmap和errorHudFrameLayout被省略了,因为它们并不直接与获取状态栏的高度有关。

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

更新::

当前的方法:

好的,状态栏的高度取决于屏幕的大小,例如在一个设备中 对于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;
} 

默认高度是25dp。在Android Marshmallow (API 23)中,高度降低到24dp。

更新:请注意,自从缺口和打孔的时代开始,使用静态高度的状态栏不再工作。请使用窗口插页代替!

官方高度是24dp 正如谷歌在Android设计网页上正式声明的那样。