Android 可靠获取状态栏高度,解决KitKat半透明导航问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20584325/
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
Reliably get height of status bar to solve KitKat translucent navigation issue
提问by Milo
I am experimenting with the new Android 4.4 translucent navigation bars and would like to set the navigation bar as translucent using the FLAG_TRANSLUCENT_NAVIGATION
flag. I only wish the navigation bar (back, home button etc) to be translucent - I want the status bar at the top of the screen to appear normally I.e. NOT translucent.
我正在试验新的 Android 4.4 半透明导航栏,并希望使用该FLAG_TRANSLUCENT_NAVIGATION
标志将导航栏设置为半透明。我只希望导航栏(返回、主页按钮等)是半透明的 - 我希望屏幕顶部的状态栏正常显示,即不透明。
The code I am using to achieve this is:
我用来实现这一目标的代码是:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
The problem I have is Android now thinks the Activity is fullscreen and places the layout behind the navigation bar (which is correct), unfortunately it also places the layout behind the status bar (a problem).
我遇到的问题是 Android 现在认为 Activity 是全屏的,并将布局放在导航栏后面(这是正确的),不幸的是它也将布局放在状态栏后面(一个问题)。
A hacky fix for this would be to apply a padding to the top of the layout parent View, however I need to determine the height of the status bar to do this.
对此的一个hacky修复是将填充应用于布局父视图的顶部,但是我需要确定状态栏的高度才能做到这一点。
Could anyone suggest how I get the status bar height, it's not as trivial as I thought it would be, or alternatively suggest a proper solution.
任何人都可以建议我如何获得状态栏高度,这并不像我想象的那么简单,或者建议一个适当的解决方案。
Thanks
谢谢
回答by user2511882
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
Use the above code in the onCreate method. Put it in a contextWrapper class. http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/
在 onCreate 方法中使用上面的代码。将它放在 contextWrapper 类中。 http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/
回答by Jacek Marchwicki
Since api 21 there is official method for retrieving insets for status bar and navigation bar height when is translucent
从 api 21 开始,有官方方法可以在半透明时检索状态栏和导航栏高度的插图
ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
final int statusBar = insets.getSystemWindowInsetTop();
final int navigationBar = insets.getSystemWindowInsetBottom();
return insets;
}
});
回答by Mete
The accepted answer always returns the status bar height (and in a somewhat hacky way). But some activities may actually be fullscreen, and this method doesn't differentiate between them.
接受的答案总是返回状态栏高度(并且以一种有点hacky的方式)。但是有些活动实际上可能是全屏的,这种方法并没有区分它们。
This method works perfectly for me to find the status bar height relative to the current activity (place it in your Activity class, and use it once layout has finished):
这个方法非常适合我找到相对于当前活动的状态栏高度(将它放在你的活动类中,并在布局完成后使用它):
public int getStatusBarHeight() {
Rect displayRect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(displayRect);
return displayRect.top;
}
Note you could also just use displayRectdirectly in case you have other "window decorations" at the bottom or potentially even the sides of the screen.
请注意,您也可以直接使用displayRect,以防您在底部甚至屏幕两侧有其他“窗口装饰”。
回答by Aritra Roy
The height of the bottom Navigation bar is 48dp (in both portrait and landscape mode) and is 42dp when the bar is placed vertically.
底部导航栏的高度为 48dp(纵向和横向模式下),垂直放置时为 42dp。
Hope this helps.
希望这可以帮助。
回答by Yogesh Rathi
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:
(旧方法)要在您的 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;
}