浅谈NavigationBar高度的获取

简介:

由于MEIZU手机的特殊性,对于NavigationBar的获取有特殊的讲究,有三个方面的因素:

1)MEIZU早期的系统(flyme os 5以下)存在smartbar,且可以设置智能隐藏;flyme os 5及以后smartbar被废弃,而使用android的NavigationBar

2)flymeos 5以下的系统有些存在系统dimen资源用于获取smartbar高度,有些则不再存在

3)是否智能隐藏SmartBar的开关并不与Android是否显示NavigationBar的开关为同一个

所以我今天研究了下,封装了一个方法:

public static int getNavigationBarHeight(Context context) {
    final boolean isMeiZu = Build.MANUFACTURER.equals("Meizu");

    final boolean autoHideSmartBar = Settings.System.getInt(context.getContentResolver(),
            "mz_smartbar_auto_hide", 0) == 1;

    if (isMeiZu) {
        if (autoHideSmartBar) {
            return 0;
        } else {
            try {
                Class c = Class.forName("com.android.internal.R$dimen");
                Object obj = c.newInstance();
                Field field = c.getField("mz_action_button_min_height");
                int height = Integer.parseInt(field.get(obj).toString());
                return context.getResources().getDimensionPixelSize(height);
            } catch (Throwable e) { // 不自动隐藏smartbar同时又没有smartbar高度字段供访问,取系统navigationbar的高度
                return getNormalNavigationBarHeight(context);
            }
        }
    } else {
        return getNormalNavigationBarHeight(context);
    }
}

protected static int getNormalNavigationBarHeight(final Context ctx) {
    try {
        final Resources res = ctx.getResources();
        int rid = res.getIdentifier("config_showNavigationBar", "bool", "android");
        if (rid > 0) {
            boolean flag = res.getBoolean(rid);
            if (flag) {
                int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    return res.getDimensionPixelSize(resourceId);
                }
            }
        }
    } catch (Throwable e) {
        LogCatLog.d("FBTools", "getNormalNavigationBarHeight() exception:" + e.getMessage());
    }
    return 0;
}

目录
相关文章
UITextView设置边框
UITextView设置边框
46 0
|
存储 iOS开发
iOS流布局UICollectionView系列五——圆环布局的实现
iOS流布局UICollectionView系列五——圆环布局的实现
222 0
iOS流布局UICollectionView系列五——圆环布局的实现
uwp - 获取当前屏幕宽高/应用宽高
原文:uwp - 获取当前屏幕宽高/应用宽高    public static Size GetScreen() { var applicationView = ApplicationView.
1066 0
|
图形学
控件渐变色的实现
控件渐变色的实现(一)—— CAGradientLayer实现控件渐变色的实现(二)—— Core Graphics实现
760 0