美文网首页
fitsSystemWindows 沉浸式

fitsSystemWindows 沉浸式

作者: bhfo | 来源:发表于2017-05-12 11:21 被阅读33次

为了自定义Status Bar和Navigation Bar的背景色。
需要设置window的uioption 为View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN。

view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

存在Navigation Bar的话,还要附加View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION,
UI才真的是“full screen”。此时navigation bar上的几个按钮图标会叠加在UI底部上。

view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

最后,设置了fitsSystemWindows的View,就会由系统重新定义其paddingTop和paddingBotton。

有个小坑,只有一个View的fitsSystemWindows生效,并且同时设置了上下的padding。如果你设置的view不需要全屏的话,那得开手动档。

 public static boolean hasNavigationBar(Context context) {
        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        return !hasMenuKey && !hasBackKey;
    }

   public static int getSystemHeight(Activity activity) {
        Resources resources = activity.getResources();
        int navigationId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        int statusId = resources.getIdentifier("status_bar_height", "dimen", "android");
        mNavigationHeight = resources.getDimensionPixelSize(navigationId);
        mStatusHeight = resources.getDimensionPixelSize(statusId);
    }

结果是 fitsSystemWindows,navigation bar,Status bar ,这三要同时考虑,少了会看起怪怪的

相关文章

网友评论

      本文标题:fitsSystemWindows 沉浸式

      本文链接:https://www.haomeiwen.com/subject/fueztxtx.html