隐藏状态栏和滚动条

时间:2019-11-01 10:44:27

标签: android android-recyclerview android-toolbar

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

之前: Showed toolbar

之后: Not hidden status bar 我希望状态栏的透明度为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>

重要:我不想做全屏活动-我希望导航栏可见。祝你有美好的一天。

1 个答案:

答案 0 :(得分:0)

执行以下操作。不要忘记在工具栏中使用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">