折叠工具栏 - 状态栏下的工具栏

问题描述:

我最近碰到了协调器布局的问题。当我尝试创建简单的折叠工具栏布局(如this示例)时,工具栏apears处于状态栏下方的屏幕截图中(在preLolipop设备上一切正常,因为应用程序不会在状态栏下绘制)。我的活动布局折叠工具栏 - 状态栏下的工具栏

toolbar under statusbar (see back arrow)

代码片段:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsingToolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <ImageView 
       android:id="@+id/imageCalculationDetail" 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:background="@drawable/ic_dummy_calculation" 
       android:fitsSystemWindows="true" 
       android:scaleType="centerCrop" 
       app:layout_collapseMode="parallax"/> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="@color/transparent" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/scroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_vertical" 
     android:clipToPadding="false" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <include layout="@layout/container"/> 

    </android.support.v4.widget.NestedScrollView> 

</android.support.design.widget.CoordinatorLayout> 

我的样式(仅V21),其中BaseAppTheme父Theme.AppCompat.Light.NoActionBar:

<style name="AppTheme" parent="BaseAppTheme"> 
     <item name="android:windowTranslucentStatus">true</item> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="android:colorAccent">@color/colorPrimary</item> 
     <item name="android:colorButtonNormal">@color/button_state_list</item> 
     <item name="android:statusBarColor">@color/transparent</item> 
    </style> 
+0

发布您的styles.xml文件。 –

+0

我已将样式添加到问题 –

+0

您的清单文件如何?你有指定正确的风格吗?也张贴那一个。因为我在棒棒糖模拟器上尝试了相同的布局和一切正常。 –

套用断点以包括一个边缘顶部的棒棒糖设备或以上的工具栏。

值/ dimens.xml

<dimen name="toolbar_margin_top">0dp</dimen> 

值-V21/dimens.xml

<dimen name="toolbar_margin_top">24dp</dimen> 

的再布局:

<android.support.v7.widget.Toolbar 
    ..... 
    android:layout_marginTop="@dimen/toolbar_margin_top" 
+0

这看起来更像是一种解决方法,我想要一些干净的解决方案,[示例]中没有顶部边距(http://antonioleiva.com/ collapsing-toolbar-layout /) –

+0

如果你想有一个透明的状态栏,这是修复它的唯一方法。否则,您需要在样式中禁用android:windowDrawsSystemBarBackgrounds属性: false

<item name="android:windowDrawsSystemBarBackgrounds">true</item> 

通过把这个在你的风格,你告诉Android,你的应用程序负责r绘制系统栏后面的内容。这就是为什么如果设置了这个标志,其他答案中的“肮脏的黑客攻击”在技术上是正确的。

除非你有一个明确的标志为真(我怀疑不是)的原因,否则将其设置为false,以便操作系统正确地为你补偿你的内容。您的状态栏应该仍然是半透明的。

+0

不设置此值为false不会产生任何影响 –

尝试通过从ImageView的android:fitsSystemWindows="true" ..

<ImageView 
      android:id="@+id/imageCalculationDetail" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:background="@drawable/ic_dummy_calculation" 
      android:scaleType="centerCrop" 
      app:layout_collapseMode="parallax"/> 
+0

删除此标志后,工具栏将处于状态栏,但状态栏停止透明和整个折叠工具栏被分离,这是不希望的效果 –

+0

你会请张贴截图吗? –

嗯,我看着办吧,问题是,我在BaseAppTheme的toolbarStyle有

<item name="android:fitsSystemWindows">true</item> 

(其他actvities,一切正常,用于折叠半透明状态栏的工具栏)。设置后

android:fitsSystemWindows="false" 

到我的工具栏,一切工作正常。

对我来说

在XML添加属性fitSystemWindows = “假” 到工具栏的工作。

<android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:fitsSystemWindows="false" 
       app:popupTheme="@style/AppTheme.PopupOverlay" 
       app:layout_collapseMode="pin"/>