【问题标题】:Status Bar (notification bar) is disappearing on scroll with CoordinatorLayout状态栏(通知栏)在使用 CoordinatorLayout 滚动时消失
【发布时间】:2016-03-02 10:44:27
【问题描述】:

我正在使用 Android Studio 中的默认模板使用 CoordinatorLayout。

但是,当我向下滚动时,通知栏(状态栏)正在滚动到屏幕外。效果可以在这个视频中看到。 https://youtu.be/1oAqEpg7N4I

我已包含相关部分,但其余代码可在此处获得: https://github.com/vidia/MaterialDiningCourts/blob/master/app/src/main/res/layout/activity_meal_view.xml

在演示 (cheesesquare) 中,行为是我所期望的,将通知栏留在原处,工具栏在其下方滚动。我已经搜索了这些布局并在我的代码中进行了更改以匹配它们,但无法找到必要的更改。

是什么导致通知栏滚动到页面外,为什么 Android Studio 中的示例会出现这种错误行为?

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MealViewActivity">

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<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"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <Spinner
            android:id="@+id/meal_chooser_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTabLayout"
        android:foregroundGravity="top"
        app:tabIndicatorColor="@color/tabSelectedIndicatorColor"/>
</android.support.design.widget.AppBarLayout>

还有styles.xml,虽然我已经从这个文件中删除了我的自定义主题,但它对这个问题没有帮助。

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <!-- Required from http://stackoverflow.com/a/29014475/2193387 -->
    <item name="android:datePickerDialogTheme">@style/dateDialogTheme</item>

</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="AppTabLayout" parent="Widget.Design.TabLayout">
    <!--<item name="tabMaxWidth">@dimen/tab_max_width</item>-->
    <item name="tabIndicatorColor">@color/tabIndicatorColor</item>
    <item name="tabIndicatorHeight">4dp</item>
    <!--<item name="tabPaddingStart">6dp</item>-->
    <!--<item name="tabPaddingEnd">6dp</item>-->
    <!--<item name="tabBackground">?attr/selectableItemBackground</item>-->
    <item name="tabTextAppearance">@style/AppTabTextAppearance</item>
    <item name="tabSelectedTextColor">@color/white</item>
</style>

<style name="AppTabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">12sp</item>
    <item name="android:textColor">@color/tabUnselectedTextColor</item>
    <item name="textAllCaps">true</item>
</style>

<!-- TODO: Change to use AppCompat rather than Material -->
<style name="dateDialogTheme" parent="android:Theme.Material.Light.Dialog">
    <!-- Required from http://stackoverflow.com/a/29014475/2193387 -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

【问题讨论】:

  • 尝试在您的AppBarLayout 上设置android:fitsSystemWindow="true"
  • 我想你的意思是在CoordinatorLayout中,AppBarLayout没有属性fitsSystemWindow。另外,我的 CoordinatorLayout 中已经有了。
  • 我的意思是fitsSystemWindows,是一个错字。不确定,看cheesesquare example,他们的ViewPagerAppBarLayout下面,试试移动吧。
  • 我试过了。 cheesesquare 示例的唯一区别是它们的协调器布局位于 DrawerLayout 中。问题是我不需要抽屉。
  • 答案是here

标签: android android-layout


【解决方案1】:

消失的状态栏效果是 values-21/styles.xml 中这些定义的结果:

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>

通过这些定义,系统不再绘制系统栏背景。您看到的状态栏背景实际上是由 CoordinatingLayout 绘制的,超出了现在透明的系统栏。现在,当您滚动时,CoordinatedLayout 会向上滚动 AppBarLayout,并且其内容通过透明的系统栏可见。

您有两种选择来解决此问题:

  • 使状态栏不透明
  • 另外使用 CollapsingToolbarLayout,并设置状态栏纱布

【讨论】:

  • 感谢您提供的提示,这些提示使我找到了解决此问题的完美解决方案here
【解决方案2】:

我也遇到了这个问题,过了一段时间我发现我必须删除

android:fitsSystemWindows="true"

来自CoordinatorLayout

我不太清楚为什么,但我认为它只是 NavigationDrawer 需要的,因为根据材料设计指南,它会覆盖状态栏,我需要将这个标签设置为 true。

希望这也对你有用。

【讨论】:

    【解决方案3】:

    将此添加到您的CoordinatorLayout

    android:fitsSystemWindows="true"
    

    或者试试这个:

        <android.support.design.widget.CoordinatorLayout 
        ...
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
        <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_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                android:minHeight="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
                ...
            </android.support.v7.widget.Toolbar>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:foregroundGravity="top"
                ... />
        </android.support.design.widget.AppBarLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 我的实现中已经有 fitSystemWindows=true 了。而且我看到您包含的代码 sn-p 没有任何变化。你能解释一下吗?
    • 我认为您的问题是关于 Toolbar 和 AppBarLayout 的主题,所以我尝试将其替换为在我的应用程序中有效的主题...
    • @Vidia 把你的 style.xml 和 style-v21.xml
    • 我进行了这些更改(尽管我有自己的主题,而您列出的作为父母的主题),但我仍然没有看到任何不同。
    • 我已经更新了代码,现在试试,如果它不起作用,编辑答案并放入 style.xml
    【解决方案4】:

    要解决它,您必须删除此行:

    android:fitsSystemWindows="true"
    

    它为我解决了问题。

    【讨论】:

      【解决方案5】:

      我设法通过删除 CoordinatorLayout 内工具栏中的以下行来摆脱这种行为:

      <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"        << this one
              app:popupTheme="@style/AppTheme.PopupOverlay">
      

      【讨论】:

      • 问题的答案不正确。有了这个,它就会停止滚动。
      猜你喜欢
      • 1970-01-01
      • 2015-04-05
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      相关资源
      最近更新 更多