【问题标题】:how can i get the screen height (pixel) without status bar & actionbar?如何在没有状态栏和操作栏的情况下获得屏幕高度(像素)?
【发布时间】:2014-04-09 13:39:19
【问题描述】:

我怎样才能在没有状态栏和操作栏的情况下获得屏幕高度(像素)或者如果有人告诉我如何获得状态栏和操作栏的高度也会很有帮助。我已经找到了屏幕高度,但它包括状态栏和操作栏.我将支持库 v7 用于操作栏。我在网上搜索,但这些解决方案对我不起作用。

【问题讨论】:

  • 使用 DisplayMetrics 类获取宽度和高度。
  • 我已经在使用这个类并且能够找到包括状态栏和操作栏在内的整个屏幕高度。请让我知道如何在没有状态栏和操作栏的情况下获得屏幕显示的高度。谢谢

标签: android window screen


【解决方案1】:

获取你喜欢的状态栏高度:

 Rect rectangle = new Rect();
 Window window = activity.getWindow();
 window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
 int statusBarTop = rectangle.top;
 int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
 int statusbarHeight = Math.abs(statusBarTop - contentViewTop);

那么操作栏呢,在 actionbarsherlock 中有一个常量值:abs__action_bar_default_height。也许你应该尝试在支持库操作栏中找到类似的东西

附言无论如何,既然你知道状态栏高度,你可以通过减去活动的主布局屏幕X值和状态栏高度值得到动作栏高度。它看起来像这样:

 mMainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {

            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 statusbarHeight = Math.abs(statusBarHeight - contentViewTop);

            int[] location = {0,0};
            mMainLayout.getLocationOnScreen(location);

            int actionbarheight = Math.abs(location[0] -statusBarHeight);
            Toast.makeText(MainActivity.this, actionbarheight + "", 1).show();
        }
    });

这是“即时”编写的,只是为了让您知道如何做到这一点。

【讨论】:

  • 我使用支持库 v7 appcompat 来显示操作栏。感谢回复
  • 这里的“contentViewTop”是状态栏和操作栏的总高度吗?
  • 不,contentViewTop 是活动屏幕的顶部(有操作栏,但没有状态栏)
  • ActionBar 有方法 getHeight 可以使用;)
猜你喜欢
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
  • 2017-05-20
相关资源
最近更新 更多