带有android棒棒糖工具栏的快速返回操作栏

时间:2014-10-31 20:41:28

标签: android android-actionbar android-5.0-lollipop android-toolbar

我正在使用AppCompat-v21的新工具栏。我想要做的是在用户开始向下滚动时隐藏操作栏,并在用户向上滚动时再次显示操作栏。它就像谷歌播放应用程序。我还在工具栏下面有一个标签布局,就像google play app一样。这需要留在那里。

我尝试过使用谷歌IO应用程序的代码。但是当工具栏消失时,它会使工具栏消失,主要内容不会向上移动。还有一个空白空间,以前工具栏在哪里。也许是因为该实现仅适用于列表和网格视图。

知道怎么做吗?

2 个答案:

答案 0 :(得分:2)

你需要做的是基本上创建自己的ActionBar主题,将windowActionBarOverlay设置为true:

<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
       parent="@android:style/Theme.AppCompat">
    <item name="android:windowActionBarOverlay">true</item>

    <!-- Support library compatibility -->
    <item name="windowActionBarOverlay">true</item>
</style>

这与听起来完全一样 - 它位于当前视图的顶部。但是,这也意味着您需要在主视图的顶部添加额外的边距以考虑ActionBar叠加层。

<!-- Support library compatibility -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">
    ...
</RelativeLayout>

一旦启动并运行自定义ActionBar主题,您就不应再看到额外的空间了。这些代码片段直接来自Android Developer Training Guide。按照链接获取有关ActionBarOverlay如何工作的更多详细信息。

答案 1 :(得分:0)

更简单的方法是使用app:layout_scrollFlags

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

因此工具栏会自动检测滚动并开始隐藏或显示。

P.S。如果您正在使用ListView,则需要将其更改为RecycleView

相关问题