Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
Android中状态栏的高度是多少?总是一样吗?
从我的测量来看,它似乎是25dp,但我不确定它是否在所有平台上都有相同的高度。
(我想知道这正确地实现一个淡出过渡从一个没有状态栏的活动到一个这样做)
当前回答
这个问题最近与我有关,因为我的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被省略了,因为它们并不直接与获取状态栏的高度有关。
其他回答
切换全屏解决方案:
这个解决方案可能看起来像一个变通方法,但它实际上解释了你的应用程序是否全屏(也就是隐藏状态栏):
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坐标无论应用是否全屏。
享受
在android6.0中,状态栏的高度为24dp
<!-- Height of the status bar -->
<dimen name="status_bar_height">24dp</dimen>
<!-- Height of the bottom navigation / system bar. -->
<dimen name="navigation_bar_height">48dp</dimen>
你可以在源代码中找到答案:frameworks\base\core\res\res\values\ dimensions .xml
根据材料指南;状态栏高度为24dp。
如果你想获得状态栏的高度,你可以使用下面的方法:
private static int statusBarHeight(android.content.res.Resources res) {
return (int) (24 * res.getDisplayMetrics().density);
}
可以从activity中调用:
statusBarHeight(getResources());
在我用来获取状态栏高度的所有代码示例中,唯一一个在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/
这个问题在… 状态栏的高度?
更新::
当前的方法:
好的,状态栏的高度取决于屏幕的大小,例如在一个设备中 对于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;
}