如何获取状态栏和软键按钮栏的高度?

时间:2015-04-01 19:12:50

标签: android statusbar softkeys

在Android中是否有办法获得软按钮栏和状态栏的大小?或者一种方法去获得没有这两个高度的屏幕高度? (它们并不总是相同的,例如看看nexus 7)

4 个答案:

答案 0 :(得分:33)

  

状态栏的高度

状态栏的高度取决于屏幕尺寸,例如,在屏幕尺寸 240 X 320 的设备中,状态栏高度 20px ,对于设备屏幕尺寸为 320 X 480 ,状态栏高度 25px ,对于 480 x 800 的设备,状态栏高度必须为38px

因此我建议使用此脚本获取状态栏高度

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

要获取活动的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;
} 
  

软键按钮栏

此方法对于在Android KitKat(4.4)中设置布局填充非常有用。使用此功能,您可以避免在布局上重叠的软按钮栏。

getRealMetrics方法仅适用于API 17和+

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

参考:

  

Height of status bar in Android

     

Dimension of soft buttons bar

答案 1 :(得分:0)

对我来说,在android note 10中,我需要按dimen获取导航栏的高度:

fun getSoftNavigationBarSize(resources: Resources): Int {
    var result = 0
    val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
    if (resourceId > 0) {
        result = resources.getDimensionPixelSize(resourceId)
    }
    return result
}

答案 2 :(得分:0)

我知道这已经得到了回答,但我刚刚发现这个解决方案似乎比其他解决方案更容易。我只在底部按钮栏上尝试过。不过,我很确定它对状态栏的工作方式相同。

view.rootWindowInsets?.let { inset ->
    Log.e("XXX", "button bar height: ${inset.systemWindowInsetBottom}")
}

注意:此时您需要一个附加到窗口的视图。

答案 3 :(得分:0)

 private void getStatusAndNavigationBarsHeight() {
        float deviceDensity = getResources().getDisplayMetrics().density;
        float statusHeight = 0;
        float navigationHeight = 0;
        Resources myResources = getApplicationContext().getResources();

        int idStatusBarHeight = myResources.getIdentifier( "status_bar_height", "dimen", "android");
        statusHeight = idStatusBarHeight > 0 ? getResources().getDimensionPixelSize(idStatusBarHeight)/deviceDensity : 0;
        statusHeight = Math.round(statusHeight);
        Log.d("StatusBar Height(dp):", String.valueOf(statusHeight));

        int idNavigationBarHeight = myResources.getIdentifier("navigation_bar_height", "dimen", "android");
        navigationHeight = idNavigationBarHeight > 0 ? getResources().getDimensionPixelSize(idNavigationBarHeight)/deviceDensity : 0;
        navigationHeight = Math.round(navigationHeight);
        Log.d("Navigation Bar Height(dp):", String.valueOf(navigationHeight));
    }