2016-04-09 34 views
4

我拥有一个能够隐藏导航栏的HTC One A9。在我的应用程序中,我需要获得导航栏的高度并设置与其对应的填充。这是我现在的问题:当我隐藏导航栏时,填充仍在设置中(即使Snapchat有这个问题)。我的问题是:是否有其他代码可以使它工作?如何真正在Android中获取导航栏高度

public static int getNavBarHeight(Context context) { 
    int result = 0; 
    int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); 
    if (resourceId > 0) { 
     result = context.getResources().getDimensionPixelSize(resourceId); 
    } 
    return result; 
} 

感谢您的帮助!

+0

你想达到什么目的?说一些关于这方面的细节。可能你根本不需要计算导航栏高度。 –

+0

@Jabbar_Jigariyo当用户有一个导航栏(我使用透明导航栏)时,我需要设置填充到包含ImageButtons和更多的底部视图 –

+0

如果您使用透明导航栏,那么为什么会需要隐藏它?对于那些有导航栏的设备,它已经是透明的了,对于那些有物理按钮的设备,不会有导航栏。对 ? –

回答

6

这是我用来获取导航栏大小的代码。它的高度将在Point.y

感谢this answer

public static Point getNavigationBarSize(Context context) { 
    Point appUsableSize = getAppUsableScreenSize(context); 
    Point realScreenSize = getRealScreenSize(context); 

    // navigation bar on the right 
    if (appUsableSize.x < realScreenSize.x) { 
     return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y); 
    } 

    // navigation bar at the bottom 
    if (appUsableSize.y < realScreenSize.y) { 
     return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y); 
    } 

    // navigation bar is not present 
    return new Point(); 
} 

public static Point getAppUsableScreenSize(Context context) { 
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    Display display = windowManager.getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    return size; 
} 

public static Point getRealScreenSize(Context context) { 
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    Display display = windowManager.getDefaultDisplay(); 
    Point size = new Point(); 

    if (Build.VERSION.SDK_INT >= 17) { 
     display.getRealSize(size); 
    } else if (Build.VERSION.SDK_INT >= 14) { 
     try { 
      size.x = (Integer)  Display.class.getMethod("getRawWidth").invoke(display); 
      size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display); 
    } catch (IllegalAccessException e) {} catch  (InvocationTargetException e) {} catch (NoSuchMethodException e) {} 
    } 

    return size; 
} 

编辑:要回答你的问题,我有,因为我想ResideMenu添加到我的应用程序使用此fonction,但最终得到一个奇怪的由于导航栏的原因,我的应用程序底部出现了空边距。

所以我编辑这fonction通过ResideMenu加入这样的:

@Override 
protected boolean fitSystemWindows(Rect insets) { 
    // Applies the content insets to the view's padding, consuming that content (modifying the insets to be 0), 
    // and returning true. This behavior is off by default and can be enabled through setFitsSystemWindows(boolean) 
    // in api14+ devices. 

    int bottomPadding = insets.bottom; 

    Point p = getNavigationBarSize(getContext()); 

    if (Build.VERSION.SDK_INT >= 21 && p.x != 0) { 
     Resources resources = getContext().getResources(); 
     int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); 
     if (resourceId > 0) { 
      bottomPadding += resources.getDimensionPixelSize(resourceId); 
     } 
    } 

    this.setPadding(viewActivity.getPaddingLeft() + insets.left, viewActivity.getPaddingTop() + insets.top, 
      viewActivity.getPaddingRight() + insets.right, viewActivity.getPaddingBottom() + bottomPadding); 
    insets.left = insets.top = insets.right = insets.bottom = 0; 
    return true; 
} 

希望这将帮助你。

+0

谢谢,以及如何我可以使用这个点在布局等填充?谢谢! –

+0

我编辑了我的答案,以显示我在代码中如何使用此点。 – ToinToin

+1

您的代码无法编译。 – JohnWatsonDev