2012-04-15 74 views
2

我遇到了需要android平板電腦系統欄高度的問題。這是與here相同的問題,但我無法使用這個答案,因爲它給了我與DisplayMetrics相同的尺寸。在這兩種方式中,我都可以通過這個欄來計算分辨率。 (這個問題並不出現在我的智能手機上,只是在我的平板電腦上)。Android平板電腦上的系統欄大小

回答

11
// status bar (at the top of the screen on a Nexus device) 
public static int getStatusBarHeight(Context context) { 
    Resources resources = context.getResources(); 
    int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); 
    if (resourceId > 0) { 
     return resources.getDimensionPixelSize(resourceId); 
    } 
    return 0; 
} 

// navigation bar (at the bottom of the screen on a Nexus device) 
public static int getNavigationBarHeight(Context context) { 
    Resources resources = context.getResources(); 
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); 
    if (resourceId > 0) { 
     return resources.getDimensionPixelSize(resourceId); 
    } 
    return 0; 
} 
+0

這兩個方法都是一樣的 – Frank 2013-11-20 13:55:28

+2

@Frank - 不同的值作爲第一個參數傳遞給'getIdentifier()'。 – 2013-11-20 20:26:58

+0

非常好:)謝謝 – dijipiji 2014-02-08 23:08:56

0

Chris Banes在最近的Droidcon談話中提到了這一點。整個談話是超級相關的,但是這裏有一個鏈接到回答你的問題的部分(包括平板電腦在內的所有形式因素)。

https://youtu.be/_mGDMVRO3iE?t=1093

這是爲了讓狀態欄和導航欄大小的正確方式:setOnApplyWindowListener

我希望這有助於!