【发布时间】:2012-04-15 20:00:03
【问题描述】:
我遇到了需要安卓平板系统栏高度的问题。这与here 的问题相同,但我不能使用这个答案,因为它给了我与 DisplayMetrics 相同的大小。在这两种方式中,我都得到了这个栏已经计算好的分辨率。 (这个问题不会出现在我的智能手机上,只会出现在我的平板电脑上)。
【问题讨论】:
我遇到了需要安卓平板系统栏高度的问题。这与here 的问题相同,但我不能使用这个答案,因为它给了我与 DisplayMetrics 相同的大小。在这两种方式中,我都得到了这个栏已经计算好的分辨率。 (这个问题不会出现在我的智能手机上,只会出现在我的平板电脑上)。
【问题讨论】:
// 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;
}
【讨论】:
getIdentifier()。
在此处查看此答案:https://stackoverflow.com/a/8367739/807499
它给出了屏幕的整个高度。然后,您可以减去 DisplayMetrics 给出的大小或根布局的大小。
【讨论】:
Chris Banes 在最近的一次 Droidcon 演讲中提到了这一点。整个演讲非常相关,但这里有一个链接可以回答您的问题(适用于所有形式,包括平板电脑)。
https://youtu.be/_mGDMVRO3iE?t=1093
这是获取状态栏和导航栏大小的正确方法:setOnApplyWindowListener
我希望这会有所帮助!
【讨论】: