【问题标题】:How to show/hide BottomAppBar programmatically?如何以编程方式显示/隐藏 BottomAppBar?
【发布时间】:2018-12-29 13:53:19
【问题描述】:

我尝试使用 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 的行为,但它不起作用。

【问题讨论】:

    标签: android kotlin material-design android-bottomappbar


    【解决方案1】:

    您可以使用其行为类中的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
    

    Java

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

    【讨论】:

    • HideBottomViewOnScrollBehavior slideUp/Down 被“保护”
    【解决方案2】:

    调用底部应用栏上的performShow()performHide() 即可满足您的需求。

    【讨论】:

    • 谢谢,它已经随 1.1.0 一起发布了。
    【解决方案3】:

    科特林

    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)
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多