android - 如何在没有状态栏和操作栏的情况下获取屏幕高度(像素)?

标签 android window screen

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

最佳答案

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

 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。也许你应该尝试在支持库操作栏中找到类似的东西

附注无论如何,既然你知道状态栏高度,你可以通过减去 Activity 的主布局屏幕 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();
        }
    });

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

关于android - 如何在没有状态栏和操作栏的情况下获取屏幕高度(像素)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22964368/

相关文章:

javascript - 使用 Ghost.py 在 python 中屏幕抓取动态网页

java - J2ME UI 画面设计

C++ 透明窗口

iPhone 正确的横向窗口坐标

android:tabStripEnabled ="false"不起作用

android - 我可以强制使用特定的布局方向,但键盘方向相反吗?安卓

widget - 您可以从小部件获取父 GTK 窗口吗?

ios - 如何使 Swift 中的所有屏幕保持一致?

android - 阻止其他应用程序访问存储在 SD 卡中的我的应用程序图像

java - 如何处理 org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs() 的结果