问题
I'm trying put my ImageBackdrop from the collapsing toolbar behind the statusBar, I've done it right with the Drawer and the navigationView but I can't do it with my fragments, In the cheesequare demo, Chris did it, and I can do it that way, but it's not using a single activity app and fragment transitions, so that's not quite what I'm looking for.
My style:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
My v21 Style:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
My Main Activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include
layout="@layout/content_main"/>
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/window_background_white"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:itemTextColor="@color/title_text_color"
app:menu="@menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>
My content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/fragment_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
My fragment:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
style="@style/AppTheme.CoordinatorLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
app:collapsedTitleTextAppearance="@style/AppTheme.CollapsingToolbar.Text.Transparent"
app:contentScrim="?attr/colorPrimaryDark"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@style/AppTheme.CollapsingToolbar.Text.Transparent"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed|enterAlways"
app:title="">
<ImageView
android:id="@+id/imageView_backdrop"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:adjustViewBounds="true"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"/>
<TextView
android:id="@+id/textView_expanded_title"
style="@style/AppTheme.CollapsingToolbar.TextView.Multiline"
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="@color/colorAccent"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
style="@style/AppTheme.RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
Another Fragment where I don't need the content behind
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
style="@style/AppTheme.CoordinatorLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="@color/colorAccent"/>
<android.support.design.widget.TabLayout
android:id="@+id/about_tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<ViewSwitcher
android:id="@+id/viewSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/progress_bar_color_primary"/>
<android.support.v4.view.ViewPager
android:id="@+id/about_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</ViewSwitcher>
</android.support.design.widget.CoordinatorLayout>
What I've accomplished:
If I put android:fitsSystemWindows="false"
in the DrawerLayout I can accomplish the desired effect in the fragment where I have the CollapsingToolbar but in the ViewPager I've a strange grey statusBar.
Desired effect accomplished badly with android:fitsSystemWindows="false"
in the drawerLayout:
But in the second fragment the content it's behind the statusBar, where I don't want it.
What I've read:
StackOverflow,
Medium
回答1:
Ok the problem was the content_main.xml it's always replaced so I thought it wasn't necessary to have the android:fitsSystemWindows attribute but i was wrong, adding android:fitsSystemWindows="true" solved the problem.
snippet content_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/fragment_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"/>
Important: if like me after that you find a bug in viewPager when coordinatorLayout has android:fitsSystemWindows="true" take a look here: CoordinatorLayout status bar padding disappears from ViewPager 2nd page
回答2:
NavigationView extends ScrimInsetsFrameLayout which does the magic. Copy paste has got you covered.
来源:https://stackoverflow.com/questions/35467576/how-to-correctly-implement-view-content-behind-statusbar-when-using-fragments