可靠获取状态栏高度,解决KitKat半透明导航问题 [英] Reliably get height of status bar to solve KitKat translucent navigation issue

查看:40
本文介绍了可靠获取状态栏高度,解决KitKat半透明导航问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验新的 Android 4.4 半透明导航栏,并希望使用 FLAG_TRANSLUCENT_NAVIGATION 标志将导航栏设置为半透明.我只希望导航栏(返回、主页按钮等)是半透明的 - 我希望屏幕顶部的状态栏正常显示,即不透明.

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.

我用来实现这一点的代码是:

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);
}

我遇到的问题是 Android 现在认为 Activity 是全屏的,并将布局放在导航栏后面(这是正确的),不幸的是它也将布局放在状态栏后面(一个问题).

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).

对此的一个hacky解决方法是在布局父视图的顶部应用填充,但是我需要确定状态栏的高度才能做到这一点.

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.

谁能建议我如何获得状态栏高度,这并不像我想象的那么简单,或者建议一个适当的解决方案.

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.

谢谢

推荐答案

public int getStatusBarHeight() {
      int result = 0;
      int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
      if (resourceId > 0) {
          result = getResources().getDimensionPixelSize(resourceId);
      }
      return result;
}

在onCreate方法中使用上面的代码.将它放在 contextWrapper 类中.http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/

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/

这篇关于可靠获取状态栏高度,解决KitKat半透明导航问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆