【问题标题】:Quick return action bar with android lollipop toolbar带有 android 棒棒糖工具栏的快速返回操作栏
【发布时间】:2014-12-28 06:28:36
【问题描述】:

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

我尝试使用来自 google IO 应用程序的代码。但是它使工具栏消失,并且在工具栏消失时主要内容不会向上移动。工具栏之前的位置仍然存在空白空间。也许是因为该实现仅适用于列表和网格视图。

知道怎么做吗?

【问题讨论】:

  • 嗨 Alam,您找到解决问题的方法了吗?
  • @Adelino 不,我没有。使用 google IO 应用程序的源代码,我设法在一定程度上做到了。仍然没有按照我想要的方式工作。
  • 嗨,Alam,在搜索了很好的示例后,我发现了这个 github.com/ksoichiro/Android-ObservableScrollView 存储库,我只是拿走了我需要的东西,它对我有用。

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


【解决方案1】:

您需要做的基本上是创建自己的 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 主题启动并运行,您应该不会再看到额外的空间。这些 sn-ps 代码直接取自 Android Developer Training Guide。按照链接获取有关 ActionBarOverlay 工作原理的更多详细信息。

【讨论】:

    【解决方案2】:

    更简单的方法是使用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"/> 
    

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

    附:如果您使用的是ListView,则需要将其更改为RecycleView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      相关资源
      最近更新 更多