2

我的布局中有两个FloatingActionButton。两者都以相同的方式附加到CoordinatorLayout。如何让SnackBar推起几个FloatingActionButton?

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawerLayout" 
    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:fitsSystemWindows="true"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

[ appbar, toolbar, content... ] 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/anon_fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="88dp" 
      android:layout_marginRight="@dimen/fab_margin" 
      android:clickable="true" 
      android:onClick="onFabClick" 
      android:src="@drawable/ic_cloud_upload_white_24dp" 
      android:tint="?colorAccent" 
      app:backgroundTint="@color/tint_inverted_fab" 
      app:borderWidth="0dp" 
      app:layout_anchor="@id/coordinatorLayout" 
      app:layout_anchorGravity="bottom|end" 
      /> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/keyboard_fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/fab_margin" 
      android:clickable="true" 
      android:onClick="onFabClick" 
      android:src="@drawable/ic_keyboard_white_24dp" 
      app:borderWidth="0dp" 
      app:layout_anchor="@id/coordinatorLayout" 
      app:layout_anchorGravity="bottom|end" 
      /> 


    </android.support.design.widget.CoordinatorLayout> 


    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/navigation_header" 
     app:menu="@menu/drawer"/> 
</android.support.v4.widget.DrawerLayout> 

但是,当我显示小吃店时,只有底部的FAB上下移动。

Snackbar.make(mCoordinator, "FAB pressed", Snackbar.LENGTH_LONG).show(); 

如何使两个移动?

回答

4

我找到的解决方案是再次实现FloatingActionButton行为并将其明确地设置在您的FAB中。

有关如何实现默认行为,BaseLab的家伙有一个很好的文章here及其匹配GitHub repo

一旦你创建你自己的这种行为重新实现,只是把它在你的布局文件是这样的:

<android.support.design.widget.FloatingActionButton 
    [...]   
    app:layout_behavior="your.package.name.FloatingActionButtonBehavior"/> 

现在按钮会动的时候你的小吃吧显示出来!