Android 4.4 — Translucent status/navigation bars — fitsSystemWindows/clipToPadding don't work through fragment transactions

前端 未结 8 2178
余生分开走
余生分开走 2021-01-30 11:30

When using the translucent status and navigation bars from the new Android 4.4 KitKat APIs, setting fitsSystemWindows=\"true\" and clipToPadding=\"false\"

8条回答
  •  我在风中等你
    2021-01-30 12:03

    I've been struggling quite a bit with this as well. I've seen all the responses here. Unfortunately none of them was fixing my problem 100% of the time. The SystemBarConfig is not working always since it fails to detect the bar on some devices. I gave a look at the source code and found where the insets are stored inside the window.

            Rect insets = new Rect();
            Window window = getActivity().getWindow();
            try {
                Class clazz = Class.forName("com.android.internal.policy.impl.PhoneWindow");
                Field field = clazz.getDeclaredField("mDecor");
                field.setAccessible(true);
                Object decorView = field.get(window);
                Field insetsField = decorView.getClass().getDeclaredField("mFrameOffsets");
                insetsField.setAccessible(true);
                insets = (Rect) insetsField.get(decorView);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
    

    This is how to get them. Apparently in Android L there'll be a nice method to get those insets but in the meantime this might be a good solution.

提交回复
热议问题