欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 沉浸式状态栏

程序员文章站 2022-06-25 09:41:07
现在市面上又很多app都实现了上滑隐藏下拉呈现视图的功能,这样可以让用户有更多的空间去浏览内容,CoordinatorLayout与AppBarLayout的功能远不止这些,大家有时间可以去探究一下,这里我只实现了上述功能。首先来看布局代码

现在市面上又很多app都实现了上滑隐藏下拉呈现视图的功能,这样可以让用户有更多的空间去浏览内容,CoordinatorLayout与AppBarLayout的功能远不止这些,大家有时间可以去探究一下,这里我只实现了上述功能。

首先来看布局代码

<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/frag_srl_content"
    android:layout_width="match_parent"
    android:layout_height="fill_parent">

    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/frag_al_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways">

                <include layout="@layout/layout_titlebar_local" />

            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.AppBarLayout>


        <FrameLayout
            android:id="@+id/frag_fl_conntent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintLeft_toLeftOf="@id/frag_al_title"
            app:layout_constraintTop_toBottomOf="@id/frag_al_title">

            <androidx.core.widget.NestedScrollView
                android:id="@+id/frg_sv_contennt"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="@dimen/dp_40"
                android:scrollbars="none">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <include
                        android:id="@+id/frg_ll_home"
                        layout="@layout/layout_home"
                        android:visibility="visible" />
                    <!--城市未运营/城市无门店/附近门店休息-->
                    <include
                        android:id="@+id/frg_ll_store_"
                        layout="@layout/layout_store_status"
                        android:visibility="gone" />
                </LinearLayout>
            </androidx.core.widget.NestedScrollView>

            <include
                android:id="@+id/frag_ll_search"
                layout="@layout/layout_search" />
        </FrameLayout>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>

大致思路就是外层放CoordinatorLayout,在里面嵌一个AppBarLayout,然后想要隐藏和呈现的视图就夹在AppBarLayout里面,这里我就放了一个ToolBar,接着要想实现滑动的功能肯定得放一个可滑动的视图,这个视图要放在AppBarLayout的外面,但是要在CoordinatorLayout的里面,这里我用的是RecyclerView(注意:一定不要用ListView,之前被整惨了。。。),在ToolBar里面要设置app:layout_scrollFlags属性,其中 Scroll 表示向下滚动时,这个View会被滚出屏幕范围直到隐藏,enterAlways 表示向上滚动时,这个View会随着滚动手势出现,直到恢复原来的位置。
这样这个功能就已经实现了,剩下的就是在Activity里面实现布局了,这里就不粘贴代码了,最后要注意的就是,使用了ToolBar要记得将Activity的主题设置成NoActionBar。

本文地址:https://blog.csdn.net/NotesChapter/article/details/107437900

相关标签: 2020-7 android