使用Android上的协调器布局是否可以使状态栏与工具栏一起滚动? [英] Is it possible to have the Status Bar scroll away along with the toolbar using coordinator layout on Android?

查看:87
本文介绍了使用Android上的协调器布局是否可以使状态栏与工具栏一起滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以滚动整个状态栏(图标和背景),而不仅是背景.几乎就像它是工具栏的一部分一样.

I would like to know if it is possible to scroll the entire status bar (icons and background), not just the background. Almost as if as if it was part of the toolbar.

我遇到了与以下问题相同的情况,不同之处是我想知道是否可以滚动整个状态栏以使背景变得不透明-这是我认为以下内容的预期结果查询

I am experiencing the same situation as the question below, the difference is I would like to know if I can scroll the entire status bar as appose to making the background opaque - which is what I think was the desired outcome of the below query

Android状态栏向上滚动使用协调器布局,使状态图标与工具栏标题重叠

这是图形

这是我的代码

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="me.hugopretorius.wishlizt.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:contentScrim="#000">

        <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.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/viewpager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tabs"/>

    <io.github.yavski.fabspeeddial.FabSpeedDial
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:fabGravity="bottom_end"
        app:fabMenu="@menu/menu_fab"
        app:miniFabBackgroundTint="@android:color/white"
        app:miniFabDrawableTint="?attr/colorPrimaryDark"
        app:miniFabTitleTextColor="?attr/colorPrimaryDark" />

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

推荐答案

工具栏"折叠后,您应该能够收听滚动更改并隐藏状态栏.这不会为您提供实际的增量滚动,但是会根据需要保留选项卡.

You should be able to listen to scroll changes, and hide the status bar once the Toolbar collapses. This won't give you actual incremental scrolling, but will leave you with just the tabs as you require.

首先,onCreate设置标志,以使当栏消失时布局不会跳转:

First, onCreate set the flags so that the layout won't jump when the bar disappears:

//root should be your coordinator/top level layout
mRoot.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                          | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

工具栏折叠后,将状态栏更改为隐藏:

Once the toolbar collapses, change the status bar to hidden:

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {

            // Collapsed
            mRoot.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                      | View.SYSTEM_UI_FLAG_FULLSCREEN); //this one changed

        } else if (verticalOffset == 0) {

            // Fully Expanded - show the status bar
            if (Build.VERSION.SDK_INT >= 16) {
                mRoot.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE
                                          | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                          | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            } else {
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }

        } else {
            // Somewhere in between
            // We could optionally dim icons in this step by adding the flag:
            // View.SYSTEM_UI_FLAG_LOW_PROFILE
        }
    }
});

这篇关于使用Android上的协调器布局是否可以使状态栏与工具栏一起滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆