2016-04-09 69 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