android - 如何以编程方式显示/隐藏 BottomAppBar?
问题描述
我尝试使用 BottomAppBar,我希望能够以编程方式隐藏或显示它,就像setExpanded
在 AppBarLayout 中一样以编程方式隐藏或显示它。
我的布局就像
<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(...)
方法。例如:
科特林
val bottomAppBar = ...
val behavior = bottomAppBar.behavior as HideBottomViewOnScrollBehavior
behavior.slideDown(bottomAppBar) // use this to hide it
behavior.slideUp(bottomAppBar) // use this to show it
爪哇
BottomAppBar bottomAppBar = ...
HideBottomViewOnScrollBehavior behavior = (HideBottomViewOnScrollBehavior) bottomAppBar.behavior;
behavior.slideDown(bottomAppBar) // use this to hide it
behavior.slideUp(bottomAppBar) // use this to show it
推荐阅读
- javascript - Javascript 警告:“^”和“>>>”非混合运算符的意外混合
- python - 无法使用 Pandas 将多个语句链接在一起
- mysql - MySQL UPDATE 中复杂的 CASE 语句
- flutter - Flutter:动态 Widget 作为另一个 Widget (B) 的参数 (A),其中 Widget (A) 接收在 Widget B 上设置的参数
- sql - 如何让代码只查看列中的某些参数?
- installation - 我无法使用 conda 将 cartopy 升级到 0.18.0
- ruby - 迭代具有有限内存和执行时间的大数组
- javascript - 即使测试表明它们确实有效,firestore 规则也不起作用
- python - 如何在 Flask 1.0.4 中启用/配置 Jinja 行语句?
- typescript - 如何将“const asserted”变量传递给接受不太严格参数的函数