使用片段时如何正确实现状态栏后面的视图内容 [英] How to correctly implement view content behind statusbar when using fragments
问题描述
我正在尝试将折叠的工具栏上的ImageBackdrop放在statusBar的后面,我已经用Drawer和navigationView正确完成了,但是我无法用我的片段来完成,在cheesequare演示中,Chris做到了,而且我可以这样做,但是它没有使用单个活动应用程序和片段过渡,所以这并不是我想要的。
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.
我的样式:
<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>
我的v21样式:
<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>
我的主要活动:
<?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>
我的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"/>
我的片段:
<?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>
我不需要后面内容的另一个片段
<?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:
如果我将 android:fitsSystemWindows = false
放在DrawerLayout中,我可以在片段中实现所需的效果,其中我有CollapsingToolbar,但是在ViewPager中,我有一个奇怪的灰色statusBar。
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.
所需的效果与 android:fitsSystemWindows = false 在抽屉布局中:
Desired effect accomplished badly with android:fitsSystemWindows="false"
in the drawerLayout:
但是在第二个片段中,内容位于statusBar后面,而我不想要它。
< a href = https://i.stack.imgur.com/UGHfYm.png rel = nofollow noreferrer>
我所读的内容:
推荐答案
好吧,问题是总是被替换的content_main.xml,所以我认为没有必要具有android:fitsSystemWindows属性,但是我错了,添加了android: fitsSystemWindows = true解决了该问题。
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.
摘要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"/>
重要提示:如果像我一样,之后您在viewPager中发现错误coordinatorLayout的android:fitsSystemWindows = true可以在此处查看:从ViewPager第二页消失的CoordinatorLayout状态栏填充
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
这篇关于使用片段时如何正确实现状态栏后面的视图内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!