状态栏的高度?

新手上路,请多包涵

有没有办法获得状态栏+标题栏的高度?检查开发论坛显示相同的问题但没有解决方案(我可以找到)。

我知道我们可以在初始布局通过后得到它,但我希望在我的活动的 onCreate() 中得到它。

谢谢

原文由 user291701 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 645
2 个回答
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;

   Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);

在你的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;
}

原文由 Jorgesys 发布,翻译遵循 CC BY-SA 3.0 许可协议

当前实际方式:

 ViewCompat.setOnApplyWindowInsetsListener(toolbar) { view, windowInsets ->
     val insets = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars())
     view.updateLayoutParams<MarginLayoutParams> {
         topMargin = insets.top
     }
     WindowInsetsCompat.CONSUMED
}

如果您想 在应用程序中支持边缘到边缘, Google 建议您这样使用。

原文由 sergpetrov 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题