【问题标题】:How to stop action bar move up如何停止动作栏向上移动
【发布时间】:2017-01-17 02:34:41
【问题描述】:

我正在使用 Android Studio 2.2.3,创建了新的选项卡项目,为每个选项卡添加了一些片段。然后为标签添加图标。一切看起来都很好。 但, 当我滚动时,特别是操作栏会上下移动。在我看来很难看。请看截图(标题和填充区域,我没有添加):

ugly screen when actionbar move up

我的问题是:

  1. 如何阻止 ActionBar 像这样上下移动?我想要 稍后处理它(可能在用户滚动时隐藏和取消隐藏它 像 Android 5.1 的 Contact App 一样查看)。
  2. 如果我不能停止运动, 我可以做什么(使用 XML 文件)来摆脱填充区域?

我刚开始学习android,如果这些问题不够好,请见谅。

这是我的主要活动布局文件(它是 Android Studio 创建的原始文件):

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

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

    </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.support.design.widget.AppBarLayout>

<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.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

谢谢。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    在你的根布局中添加android:fitsSystemWindows="true",即修改如下:

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <!-- rest of your layout here -->
    
    </android.support.design.widget.AppBarLayout>
    

    更多关于 android:fitsSystemWindows 的信息来自 Android 文档:

    根据系统窗口调整视图布局的内部属性,例如 作为状态栏。如果为真,则调整此视图的填充以离开 系统窗口的空间。仅当此视图位于 非嵌入式活动。

    必须是布尔值,“真”或“假”。

    这也可能是对资源的引用(形式为 “@[package:]type:name”)或主题属性(形式为 "?[package:][type:]name") 包含此类型的值。

    这个对应全局属性资源符号 适合SystemWindows。

    对于滚动问题,请从片段布局中删除以下行 -

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    

    从你的主要活动的布局中,从工具栏标签下删除以下行 -

        app:layout_scrollFlags="scroll|enterAlways"
    

    【讨论】:

    • 我按照你说的做了,但还是不行。 here my project。当操作栏处于正常位置时,最后一项不显示。
    • 我下载了您的源代码并尝试了一些方法来使其正常工作。我已经编辑了我的答案。另请注意,请尝试使用 recyclerview 而不是 listview。这是建议的替代方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多