2014-04-09 32 views
2

我怎样才能得到没有状态栏的屏幕高度(像素)&操作栏或者如果任何机构告诉如何获得状态栏和操作栏的高度,我也会有所帮助。我已经找到了屏幕高度,但它包括状态栏的操作栏&。我使用支持库v7的行动bar.I在网上搜索,但这些解决方案不适合我。如何获得没有状态栏和操作栏的屏幕高度(像素)?

+0

使用DisplayMetrics类获取宽度和高度。 – Loganathan

+0

我已经使用此课程并能够找到整个屏幕高度,包括状态栏和操作栏。请让我知道如何得到没有状态栏和操作栏的屏幕高度。感谢 – user3302257

回答

4

GET状态条的高度,你可以喜欢:

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。也许你应该尝试在支持库中找到那样的动作吧

p.s.无论如何,由于您知道状态栏高度,因此您可以通过减去活动的主布局屏幕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(); 
     } 
    }); 

这是写在“飞”,只是给你一个想法如何做到这一点。

+0

我使用支持库v7 appcompat显示操作栏。感谢您的回复 – user3302257

+0

在这里“contentViewTop”状态栏和操作栏的总高度? – user3302257

+0

否,contentViewTop是活动屏幕的顶部(带有操作栏,但没有状态栏) – OFFmind

相关问题