When you add a
FloatingActionButton
as a child of yourCoordinatorLayout
and then pass thatCoordinatorLayout
to yourSnackbar.make()
call - instead of the snackbar displaying over the floating action button, theFloatingActionButton
... automatically move upward as the snackbar animates in and returns to its position when the snackbar animates out
我完全按照那里的描述做了,但 FAB 没有向上移动。 (Snackbar 可以滑出,说明 CoordinatorLayout 知道。)
更新
布局:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/action_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.ActionBar"/>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/action_toolbar"/>
</RelativeLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="?attr/actionBarSize"
android:background="@color/theme_primary_darker_color"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/toolbar_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_navigation_white_24dp"
android:layout_margin="16dp"
android:visibility="gone"
app:borderWidth="0dp"
app:elevation="6dp"
app:layout_anchor="@id/action_toolbar"
app:layout_anchorGravity="bottom|right|end"
app:pressedTranslationZ="12dp"/>
</android.support.design.widget.CoordinatorLayout>
代码:
mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_content);
...
Snackbar.make(mCoordinatorLayout, R.string.waypoint_deleted, Snackbar.LENGTH_LONG).show();
最佳答案
我已经根据您发布的代码编写了示例项目,我不得不更改三处内容以使 FloatingActionButton
知道 Snackbar
。
- 将我构建的“minifyEnabled”属性设置为
false
- 看起来 Proguard 删除了一些注释(例如Behaviour
),这些注释是协调FloatingActionButton< 所必需的
。您需要在启用 Proguard 的发布版本中添加一些 Proguard 规则。 - 将
FloatingActionButton
作为Snackbar.make()
的第一个参数传递,而不是CoordinatorLayout
本身,如前所述here . - 删除
app:layout_anchor="@id/action_toolbar"
并将app:layout_anchorGravity="bottom|right|end"
替换为app:layout_gravity= "bottom|right|end"
从布局(不确定它是否适合你)。
如果它不适合你,你可以尝试编写你自己的 Behavior
类,如上面提到的文章中所述。
关于android - 显示 snackbar 时,来自设计支持库的 FAB 不会向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32141315/