Android中状态栏的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3407256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Height of status bar in Android
提问by hpique
What's the height of the status bar in Android? Is it always the same?
Android中状态栏的高度是多少?它总是一样吗?
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)
(我想知道这一点以正确实现从没有状态栏的活动到有状态栏的活动的淡入淡出过渡)
回答by Jorgesys
this question was answered before... Height of statusbar?
这个问题之前已经回答过...... 状态栏的高度?
Update::
更新::
Current method:
当前方法:
ok, the height of the status bar depends on the screen size, for example in a device with 240 X 320 screen size the status bar height is 20px, for a device with 320 X 480 screen size the status bar height is 25px, for a device with 480 x 800 the status bar height must be 38px
好的,状态栏的高度取决于屏幕尺寸,例如在屏幕尺寸为 240 X 320 的设备中,状态栏高度为 20px,对于屏幕尺寸为 320 X 480 的设备,状态栏高度为 25px,对于480 x 800 的设备状态栏高度必须为 38px
so i recommend to use this script to get the status bar height
所以我建议使用这个脚本来获取状态栏高度
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);
(old Method) to get the Height of the status bar on the onCreate()
method of your Activity, use this method:
(旧方法)在onCreate()
你的Activity的方法上获取状态栏的高度,使用这个方法:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
回答by Ben Clayton
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:
在我用来获取状态栏高度的所有代码示例中,唯一一个实际似乎可以在onCreate
an 方法中工作的代码示例Activity
是:
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. The above code can be added to a ContextWrapper
class (e.g. an Activity
).
显然,状态栏的实际高度是作为 Android 资源保留的。上面的代码可以添加到一个ContextWrapper
类中(例如一个Activity
)。
Found at http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/
发现于http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/
回答by Grantland Chew
On MDPI devices, the status bar is 25px. We can use this as the base and multiply it by the density (rounded up) to get the status bar height on any device:
在 MDPI 设备上,状态栏是 25px。我们可以使用它作为基础并将其乘以密度(向上取整)以获得任何设备上的状态栏高度:
int statusBarHeight = Math.ceil(25 * context.getResources().getDisplayMetrics().density);
For reference: ldpi=.75, mdpi=1, hdpi=1.5, xhdpi=2
供参考:ldpi=.75,mdpi=1,hdpi=1.5,xhdpi=2
回答by Niklas
Hardcoding the size or using reflection to get the value of status_bar_height
is considered bad practice. Chris Banes talked about this in at the Droidcon New York. The recommended way of getting the status bar size is via the OnApplyWindowInsetsListener:
硬编码大小或使用反射来获取值status_bar_height
被认为是不好的做法。Chris Banes在纽约 Droidcon 上谈到了这一点。获取状态栏大小的推荐方法是通过OnApplyWindowInsetsListener:
myView.setOnApplyWindowInsetsListener { view, insets -> {
val statusBarSize = insets.systemWindowInsetTop
return insets
}
This was added in API 20 and is also backported via ViewAppCompat.
这是在 API 20 中添加的,也通过ViewAppCompat向后移植。
回答by android developer
I've merged some solutions together:
我已经合并了一些解决方案:
public static int getStatusBarHeight(final Context context) {
final Resources resources = context.getResources();
final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
return resources.getDimensionPixelSize(resourceId);
else
return (int) Math.ceil((VERSION.SDK_INT >= VERSION_CODES.M ? 24 : 25) * resources.getDisplayMetrics().density);
}
another alternative:
另一种选择:
final View view = findViewById(android.R.id.content);
runJustBeforeBeingDrawn(view, new Runnable() {
@Override
public void run() {
int statusBarHeight = getResources().getDisplayMetrics().heightPixels - view.getMeasuredHeight();
}
});
EDIT: Alternative to runJustBeforeBeingDrawn: https://stackoverflow.com/a/28136027/878126
编辑:runJustBeforeBeingDrawn 的替代方法:https://stackoverflow.com/a/28136027/878126
回答by Rashid
According to Google design guidelines; height of status bar is 24 dp.
根据谷歌设计指南;状态栏的高度为 24 dp。
If you want get status bar height in pixels you can use below method:
如果您想以像素为单位获取状态栏高度,您可以使用以下方法:
private static int statusBarHeight(android.content.res.Resources res) {
return (int) (24 * res.getDisplayMetrics().density);
}
which can be called from activity with:
可以从活动中调用:
statusBarHeight(getResources());
回答by crysxd
The default height used to be 25dp. With Android Marshmallow (API 23) the height was reduced to 24dp.
默认高度曾经是 25dp。使用 Android Marshmallow (API 23),高度减少到 24dp。
Update: Please be aware that since the age of notches and punch-whole-cameras began, using a static height for the status bar no longer works. Please use window insets instead!
更新:请注意,由于缺口和打孔相机的时代开始,状态栏不再使用静态高度。请改用窗口插图!
回答by Parag Chauhan
回答by benhylau
I have the same problem of having to get the status bar height in an onCreate. This works for me.
我有同样的问题,必须在 onCreate 中获取状态栏高度。这对我有用。
private static final int LOW_DPI_STATUS_BAR_HEIGHT = 19;
private static final int MEDIUM_DPI_STATUS_BAR_HEIGHT = 25;
private static final int HIGH_DPI_STATUS_BAR_HEIGHT = 38;
Inside the onCreate:
在 onCreate 内部:
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
int statusBarHeight;
switch (displayMetrics.densityDpi) {
case DisplayMetrics.DENSITY_HIGH:
statusBarHeight = HIGH_DPI_STATUS_BAR_HEIGHT;
break;
case DisplayMetrics.DENSITY_MEDIUM:
statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
break;
case DisplayMetrics.DENSITY_LOW:
statusBarHeight = LOW_DPI_STATUS_BAR_HEIGHT;
break;
default:
statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
}
See:
看:
http://developer.android.com/reference/android/util/DisplayMetrics.htmlhttp://developer.android.com/guide/practices/ui_guidelines/icon_design.html
http://developer.android.com/reference/android/util/DisplayMetrics.html http://developer.android.com/guide/practices/ui_guidelines/icon_design.html
回答by Joaquin Iurchuk
Official height is 24dp
,
as is stated officially by Google on Android Design webpage.
官方高度是24dp
,正如谷歌在Android 设计网页上正式声明的那样。