简体   繁体   中英

Android : Fullscreen system visibility screws up window insets

I'm writing an app that has one activity and a bunch of fragments and one of those fragments is fullscreen. In order to achieve that, I'm trying to use the System UI Visibility flags.

For the fullscreen fragment, I set the visibility:

layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

As soon as I do that, the window insets change to values that don't seem to make any sense. Then, when I exit that fullscreen fragment, I wind up with wrong padding on my view (based on the weird insets)

在此输入图像描述

Does anyone know what's going on here? The way I understand the window insets must be wrong, because this behavior seems completely counter intuitive. Any insight would be appreciated.

To view the full source of the test app I wrote to illustrate the problem go here:

https://github.com/dapp/visibilitytest

setSystemUiVisibility() propagates the system UI visibility changes up the view hierarchy (to request new insets).

However, when you call layout.setSystemUiVisibility() in your Fragment's onCreateView() , the layout is not yet attached to the view hierarchy. This is what causes your issues with the insets not being updated appropriately.

You can instead call getActivity().getWindow().getDecorView().setSystemUiVisibility() in onCreateView() and you'll find that all insets work correctly.

I have the same problem today. I am using a NavigationView from design library. When I exit full screen, there are insects on top and bottom of NavigationView, which is ugly.

I debug and dig into the source code of NavigationView and find following solution:

  1. Please use setFitsSystemWindows(false) method for your view, in my case, for NavigationView, to avoid impact of status bar and navigation bar changes.

  2. I found there is OnApplyWindowInsetsListener in NavigationView's super class: ScrimInsetsFrameLayout, then I clear this listener to avoid impact when full screen is enabled/disabled, by using ViewCompat.setOnApplyWindowInsetsListener(mNavigationView, null).

Now the NavigationView behaves correctly when exit full screen, hope this helps for your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM