【问题标题】:How do I hide Material Bottom App Bar when Coordinator Layout's child fragment is scrolled?滚动协调器布局的子片段时,如何隐藏 Material Bottom App Bar?
【发布时间】:2020-05-30 23:33:30
【问题描述】:

我想知道为什么app:hideOnScroll="true" 在我的情况下不起作用,当我希望在滚动时隐藏我的BottomAppBar。 我想实现这样的目标:Hide bottom app bar on scroll

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorBackgroundPrimary"
            android:theme="@style/customtoolbar"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:titleTextColor="@color/colorTextPrimary" />

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

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

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:backgroundTint="@color/colorBtn"
        app:hideOnScroll="true"
        app:layout_scrollFlags="scroll|enterAlways"
        app:navigationIcon="@drawable/baseline_menu_24"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorPrimaryDark"
        android:src="@drawable/baseline_add_24"
        app:layout_anchor="@id/bar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="@color/colorBackgroundPrimary"
    android:backgroundTint="@color/colorBackgroundPrimary"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android xml material-design material-ui material-components-android


    【解决方案1】:

    content_main.xml 的根元素应该是NestedScrollViewRecyclerView 如果你想启用hideOnScroll

    【讨论】:

    • 我将屋顶元素更改为nestedScrollView,但现在我根本无法滚动
    • 问题是我将ConstraintLayout 完全替换为NestedScrollView!只要我在NestedScrollViewFrameLayout 之间添加了ConstraintLayout ,它就起作用了。所以content_main.xml 的层次结构现在是:NestedScrollView > ConstraintLayout > FrameLayout。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    相关资源
    最近更新 更多