簡體   English   中英

如何以編程方式顯示/隱藏BottomAppBar?

[英]How to show/hide BottomAppBar programmatically?

我嘗試使用BottomAppBar,我希望能夠像setExpanded中的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(...)方法。 例如:

科特林

 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

在底部應用欄上調用performShow()performHide()會做你想做的。

科特林

class HideBottomNavigationOnScrollBehavior<V : View>(
        context: Context?,
        attrs: AttributeSet?
) : HideBottomViewOnScrollBehavior<V>(context, attrs) {

    public override fun slideDown(child: V) {
        super.slideDown(child)
    }

    public override fun slideUp(child: V) {
        super.slideUp(child)
    }   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM