如何在没有状态栏和操作栏的情况下获取屏幕高度(像素)或者如果有人告诉我如何获取状态栏和操作栏的高度,它也会有所帮助。我已经找到了屏幕高度,但它包括状态栏和操作栏.我将支持库 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/