首页 > 解决方案 > 使用滚动工具栏隐藏状态栏

问题描述

当我在片段内滚动 RecyclerView 时,我想用工具栏隐藏状态栏。我现在只能隐藏工具栏。也许有人知道该怎么做?

前: 显示的工具栏

之后: 不隐藏状态栏 我希望状态栏的不透明度为 0% 或 50% - 而不是图片中的 100%。

main_activity.xml

<androidx.coordinatorlayout.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:background="@android:color/white"
    tools:context=".main.MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/mainAppBarLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/mainToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimaryDark"
            android:elevation="4dp"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:title="@string/app_name"
            app:titleTextColor="@android:color/white"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/mainNav"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@android:color/white"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/user_tabs_menu" >

        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:alpha="0.5"
            android:background="@color/colorGray"
            app:layout_constraintBottom_toTopOf="@+id/mainNav" />
    </com.google.android.material.bottomnavigation.BottomNavigationView>

    <FrameLayout
        android:id="@+id/mainFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="55dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toTopOf="@+id/view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mainToolbar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我不想进行全屏活动 - 我希望导航栏可见。祝你今天过得愉快。

标签: androidandroid-recyclerviewandroid-toolbar

解决方案


像下面这样。不要忘记在工具栏中使用 app:layout_scrollFlags,为了更好地滚动,您可以使用 NestedScrollView

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <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/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />


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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

推荐阅读