如何获取状态栏和软键按钮栏的高度? [英] How Get height of the Status Bar and Soft Key Buttons Bar?

查看:191
本文介绍了如何获取状态栏和软键按钮栏的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Android中获得的软按钮栏和状态栏togheter的大小?还是有办法去获得屏幕高度没有这两个栏的高度? (它们并不总是一样的,看关系7为例)

解决方案
  

状态栏的高度

在状态栏的高度在设备取决于屏幕的大小,例如使用的 240×320 的屏幕大小在状态栏高度的 20像素的,用于设备与 320×480 的屏幕大小在状态栏高度的 25像素的,与 480 * 800 的状态栏高度必须38px

所以我建议使用这个脚本来获取状态栏高度

  rect矩形=新的矩形();
窗口窗口= getWindow();
。window.getDecorView()getWindowVisibleDisplayFrame(矩形);
INT statusBarHeight = rectangle.top;
INT contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
INT titleBarHeight = contentViewTop  -  statusBarHeight;

Log.i(***价值::,状态栏高度=+ statusBarHeight +,TitleBar中高度=+ titleBarHeight);
 

让您的活动的的onCreate()法的状态栏的高度,用这个方法:

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

  

软键按钮栏

以设置布局填充在机器人奇巧(4.4)此方法是非常有用的。用这种方法,就可以避免软按钮栏重叠在你的布局。

getRealMetrics 方法仅适用于API 17 +

  @燮pressLint(NewApi)
私人诠释getSoftbuttonsbarHeight(){
    仅适用于API 17 // getRealMetrics +
    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.JELLY_BEAN_MR1){
        DisplayMetrics指标=新DisplayMetrics();
        。getWindowManager()getDefaultDisplay()getMetrics(度量)。
        INT usableHeight = metrics.heightPixels;
        。getWindowManager()getDefaultDisplay()getRealMetrics(度量)。
        INT realHeight = metrics.heightPixels;
        如果(realHeight> usableHeight)
            返回realHeight  -  usableHeight;
        其他
            返回0;
    }
    返回0;
}
 

参考:

  

在Android的的状态栏高度

     

的软按钮二维条码

Is there a way in Android to get the size of the soft buttons bar and status bar togheter? or a way to go get the screen height without the height of both these bars? (they are not always the same, look at the nexus 7 for example)

解决方案

Height of the Status Bar

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

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("*** Value :: ", "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight); 

to get the Height of the status bar on the onCreate() method of your Activity, use this method:

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

Soft Key Buttons Bar

This method is very useful in order to set the layout padding in Android KitKat (4.4). Using this, you can avoid the soft buttons bar overlapping over your layout.

The getRealMetrics method is only available with API 17 and +

@SuppressLint("NewApi")
private int getSoftbuttonsbarHeight() {
    // getRealMetrics is only available with API 17 and +
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight)
            return realHeight - usableHeight;
        else
            return 0;
    }
    return 0;
}

Reference:

Height of status bar in Android

Dimension of soft buttons bar

这篇关于如何获取状态栏和软键按钮栏的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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