android - 如何以编程方式显示/隐藏 BottomAppBar?

标签 android kotlin material-design android-bottomappbar

我尝试使用 BottomAppBar,我希望能够像 AppBarLayout 中的 setExpanded 一样以编程方式隐藏或显示它。

我的布局是这样的

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nested"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <fragment
            android:id="@+id/navHost"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/home_nav" />
    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="center"
        app:hideOnScroll="true"
        app:layout_scrollFlags="scroll|enterAlways"
        app:navigationIcon="@drawable/ic_menu"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我尝试使用 AppBottomBar 的行为,但它不起作用。

最佳答案

您可以使用其行为类中的 slideUp(...)slideDown(...) 方法。例如:

Kotlin

 val bottomAppBar = ...
 val behavior = bottomAppBar.behavior as HideBottomViewOnScrollBehavior
 behavior.slideDown(bottomAppBar) // use this to hide it
 behavior.slideUp(bottomAppBar) // use this to show it

Java

 BottomAppBar bottomAppBar = ...
 HideBottomViewOnScrollBehavior behavior = (HideBottomViewOnScrollBehavior) bottomAppBar.behavior;
 behavior.slideDown(bottomAppBar) // use this to hide it
 behavior.slideUp(bottomAppBar) // use this to show it

关于android - 如何以编程方式显示/隐藏 BottomAppBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51456312/

相关文章:

android - 如何在同一个 fragment 中创建多个 Retrofit 回调 - android

android - 带 map 的 Kotlin (标记未显示)

spring - Noarg构造函数未创建

android - 将图标与工具栏图标对齐 - Android Material Design

css - Angular Material 输入并在一个表单字段中选择

html - 更改屏幕大小时,文本和按钮会移位

android - react native 编译报错android - react-native-webview

没有对话框的 Android 云打印

Android:在绘制之前获取 View 的高度

java - 动态添加 View 并调整父 ViewGroup 的大小