本文翻译自:Height of status bar in Android
What's the height of the status bar in Android? Android中状态栏的高度是多少? Is it always the same? 总是一样的吗?
From my measurements it seems that it's 25dp, but I'm not sure if it has the same height on all platforms. 根据我的测量结果,它似乎是25dp,但我不确定它是否在所有平台上具有相同的高度。
(I want to know this to properly implement a fade transition from an activity that doesn't have status bar to one that does) (我想知道这是为了正确实现从没有状态栏的活动到执行状态栏的活动的淡入淡出过渡)
#1楼
参考:/question/EINk/Android中状态栏的高度
#2楼
Out of all the code samples I've used to get the height of the status bar, the only one that actually appears to work in the onCreate
method of an Activity
is this: 在我用来获取状态栏高度的所有代码示例中,实际看起来在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;
}
Apparently the actual height of the status bar is kept as an Android resource. 显然,状态栏的实际高度保留为Android资源。 The above code can be added to a ContextWrapper
class (eg an Activity
). 上面的代码可以添加到ContextWrapper
类(例如Activity
)。
Found at /blog/2012/03/17/get-the-height-of-the-status-bar-in-android/ 发现于/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/
#3楼
this also work with the refrence link 这也适用于参考链接
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
#4楼
To solve this, I used a combination approach. 为了解决这个问题,我使用了一种组合方法。 This is necessary as on tablets the system bar already subtracts it's pixels when () is called. 这是必要的,因为在平板电脑上系统栏已经在调用()时减去它的像素。 So I first check if a system bar is present, and then Ben Claytons approach, which works fine on phones. 所以我首先检查是否存在系统栏,然后Ben Claytons接近,这在手机上运行良好。
public int getStatusBarHeight() {
int statusBarHeight = 0;
if (!hasOnScreenSystemBar()) {
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
}
return statusBarHeight;
}
private boolean hasOnScreenSystemBar() {
Display display = getWindowManager().getDefaultDisplay();
int rawDisplayHeight = 0;
try {
Method getRawHeight = ("getRawHeight");
rawDisplayHeight = (Integer) (display);
} catch (Exception ex) {
}
int UIRequestedHeight = ();
return rawDisplayHeight - UIRequestedHeight > 0;
}
#5楼
I've merged some solutions together: 我将一些解决方案合并在一起:
public static int getStatusBarHeight(final Context context) {
final Resources resources = ();
final int resourceId = ("status_bar_height", "dimen", "android");
if (resourceId > 0)
return (resourceId);
else
return (int) ((VERSION.SDK_INT >= VERSION_CODES.M ? 24 : 25) * ().density);
}
another alternative: 另一种选择:
final View view = findViewById();
runJustBeforeBeingDrawn(view, new Runnable() {
@Override
public void run() {
int statusBarHeight = getResources().getDisplayMetrics().heightPixels - ();
}
});
EDIT: Alternative to runJustBeforeBeingDrawn: /a/28136027/878126 编辑:runJustBeforeBeingDrawn的替代方案: https :///a/28136027/878126
#6楼
Toggled Fullscreen Solution: 切换全屏解决方案:
This solution may look like a workaround, but it actually accounts for whether your app is fullscreen (aka hiding the status bar) or not: 此解决方案可能看起来像一种解决方法,但它实际上会说明您的应用是否是全屏(也就是隐藏状态栏):
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point(); (size);
int barheight = - findViewById().getHeight();
This way, if your app is currently fullscreen, barheight
will equal 0. 这样,如果您的应用当前是全屏,则barheight
将等于0。
Personally I had to use this to correct absolute TouchEvent coordinates to account for the status bar as so: 就个人而言,我必须使用它来纠正绝对的TouchEvent坐标,以便考虑状态栏,如下所示:
@Override
public boolean onTouch(View view,MotionEvent event) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point(); (size);
int YCoord = (int)() - + ());
}
And that will get the absolute y-coordinate whether the app be fullscreen or not. 无论应用程序是否全屏,这都将获得绝对的y坐标。
Enjoy 请享用