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

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

问题描述

我尝试用全新的Andr​​oid 4.4半透明导航栏,并想设置使用 FLAG_TRANSLUCENT_NAVIGATION 标记导航栏半透明。我只希望导航栏(后退,主页按钮等)是半透明的 - 我想在屏幕顶部的状态栏通常出现即不透亮。

在code我用来实现这一目标是:

 如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.KITKAT){
    窗口瓦特= getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
 

我的问题是安卓认为现在的活动是全屏,并把导航栏后面的布局(这是正确的),不幸的是它还将在状态栏(有问题)背后的布局。

本哈克解决将是一个填充应用到布局父视图的顶部,但是我需要确定状态栏的高度做到这一点。

任何人都可以建议我如何让状态栏的高度,它不是小事,因为我认为这将是,或者提出一个妥善的解决办法。

感谢

解决方案

 公众诠释getStatusBarHeight(){
      INT结果为0;
      INT RESOURCEID = getResources()则getIdentifier(status_bar_height,地扪,机器人)。
      如果(RESOURCEID大于0){
          。结果= getResources()getDimensionPixelSize(RESOURCEID);
      }
      返回结果;
}
 

使用上述code。在onCreate方法。把它放在一个contextWrapper类。 <一href="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/

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

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

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.

Thanks

解决方案

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/

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

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