Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
当前回答
默认高度是25dp。在Android Marshmallow (API 23)中,高度降低到24dp。
更新:请注意,自从缺口和打孔的时代开始,使用静态高度的状态栏不再工作。请使用窗口插页代替!
其他回答
试试这个:
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的测量
官方高度是24dp 正如谷歌在Android设计网页上正式声明的那样。
这个问题最近与我有关,因为我的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被省略了,因为它们并不直接与获取状态栏的高度有关。
在我用来获取状态栏高度的所有代码示例中,唯一一个在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/
这也适用于引用链接
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}