Translucent/Transparent status bar + CoordinatorLayout + Toolbar + Fragment

后端 未结 8 1151
难免孤独
难免孤独 2020-12-05 07:23

I have following setup:

  • I\'m using AppCompat
  • MainActivity, that holds a fragment and has a toolbar, that\'s hiding when scrolling down
8条回答
  •  隐瞒了意图╮
    2020-12-05 07:48

    tl;dr Set android:fitsSystemWindows="false" at least to the root CoordinatorLayout and to the inner fragment container, @frame_container.

    This might not be the final solution (i.e. there might be other fitsSystemWindows to change) so tell me if you have any problems.

    why

    When it comes to status bar, I think of fitsSystemWindows like so:

    • fitsSystemWindows="false" : draws the view normally, under the status bar because of the window flags you have set.
    • fitsSystemWindows="true" : draws the view normally, under the status bar because of the window flags you have set, but adds a top padding so that content is drawn below the status bar and they don't overlap.

    In fact, in my opinion, the white you see is not the status bar color, but rather your CoordinatorLayout background. That is due to fitsSystemWindows="true" on the coordinator: it draws the background to the whole window, but adds top padding to the content so inner views are not covered by status bar.

    This is not what you want. Your inner content must be covered by the status bar, and so you have to set fitsSystemWindows="false" to the coordinator (so it won't apply top padding) and probably to the content itself.

    Once you get how it works, it is easy to debug and achieve the effect you are looking for. Actually, it is not. Years pass, but I still spend hours trying to figure out the right fitsSystemWindows combination, because most Views (in the support libraries at least) override the default behavior that I stated above, in ways that are mostly not intuitive. See this post for a small guide on how to use it.

提交回复
热议问题