简体   繁体   中英

Draw behind status bar but not behind system navigation bar with WindowCompat.setDecorFitsSystemWindows(window, false)

I want to draw my layout behind the status bar but not behind system navigation controls panel (at the bottom of display)

Right now I use WindowCompat.setDecorFitsSystemWindows(window, false) and layout takes all space of the display, but I only need to take space of status bar

So basically layout should ignore top inset (where status bar is) but it needs to pay attention to bottom inset (where system navigation bar is)

I could use <item name="android:windowTranslucentStatus">true</item> instead but this one draws "system dark transparent background" for status bar which I don't want to.

So I need something like <item name="android:windowTranslucentStatus">true</item> but without dark background with transparency for status bar

For my case I solved it like this (for layout of Activity):

It needs two CoordinatorLayout s, the first with android:fitsSystemWindows="false" and the second with android:fitsSystemWindows="true"

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false"
    tools:context=".ui.MainActivity">

    <FrameLayout
        android:id="@+id/content_with_behind_status_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        ... content which draws behind status bar as well
        
    </FrameLayout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <FrameLayout
            android:id="@+id/nav_host_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" >
            
            ... content which draws below status bar and below appbar if needed (set layout_behavior)
            
        </FrameLayout>

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.MaterialComponents.ActionBar">

            <com.google.android.material.appbar.MaterialToolbar
                ... />

        </com.google.android.material.appbar.AppBarLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

No need to use WindowCompat.setDecorFitsSystemWindows(window, false) and <item name="android:windowTranslucentStatus">true</item>

But it works only for specific cases.

In my case it was activity where it should place some content and it should be visible behind status bar as well but other content is being added by fragments to this activity and it's below status bar

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