使用CoordinatorLayout滚动时Android工具栏消失

8
当我试图使用Coordinatorlayout实现工具栏时,发生了一件奇怪的事情。
  1. 当我向下滚动并向上滚动时,toolbar正确显示,一切看起来都没问题。

enter image description here

  1. 但是,当我向下滚动到一定程度后,屏幕变成了

enter image description here

以下是XML代码,请问有什么想法,谢谢?

<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.support.v7.widget.RecyclerView
    android:id="@+id/list"
    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.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_scrollFlags="scroll|enterAlways"
        />

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


@MarcinOrlowski 是的.. 但是当向下滚动到一定程度时,工具栏会消失并显示一个空白视图,结果不正确。 - Kenny
你尝试使用NoActionBar主题了吗? - TheoK
我也遇到过这个问题,仅在M版本之前出现(编译23支持设计23)。我使用NoActionBar主题。真的很奇怪。下面张贴的无形视图技巧似乎可行(在N5 4.4.4上测试过)。 - Philippe David
2个回答

15
AppBarLayout结束语句之前,在你的Toolbar下方放置一个空视图。当我遇到这个问题时,它对我有用。
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_scrollFlags="scroll|enterAlways" />

    <View
        android:id="@+id/appbar_bottom"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/transparent"
        android:visibility="invisible" />
</android.support.design.widget.AppBarLayout>

1
在最新的支持库版本(23.0.1)中似乎不再需要这个了。我之前使用了这个解决方案,但是在23.0.1中,这个bug看起来已经被修复了。 - jguerinet
@jguerinet 感谢您的更新。这样可以避免大家的麻烦。您可以将其作为类似问题的答案添加到此问题中,以便通知其他人。 - McGuile
1
哇,谢谢你啊,我在28.0.2版本中也遇到了同样的问题,你的解决方案帮了我大忙 :) - Oleksandra

0

尝试使用app:liftOnScroll="false"。 在我的情况下有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接