System bar size on android tablets

二次信任 提交于 2019-12-03 09:01:48
// 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;
}
David Scott

Check out this answer here: https://stackoverflow.com/a/8367739/807499

It gives the full height of the screen. You can then minus the size given by DisplayMetrics or by your root layout's size.

Chris Banes mentioned this in a recent Droidcon talk. The whole talk is super relevant, but here's a link to the part that answers your question (for all form factors, including tablet).

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

This is the correct way to get status bar and navigation bar sizes: setOnApplyWindowListener

I hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!