android - 显示 snackbar 时,来自设计支持库的 FAB 不会向上移动

标签 android android-layout android-design-library

This post说:

When you add a FloatingActionButton as a child of your CoordinatorLayout and then pass that CoordinatorLayout to your Snackbar.make() call - instead of the snackbar displaying over the floating action button, the FloatingActionButton ... 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

  1. 将我构建的“minifyEnabled”属性设置为 false - 看起来 Proguard 删除了一些注释(例如 Behaviour),这些注释是协调 FloatingActionButton< 所必需的。您需要在启用 Proguard 的发布版本中添加一些 Proguard 规则。
  2. FloatingActionButton 作为Snackbar.make() 的第一个参数传递,而不是CoordinatorLayout 本身,如前所述here .
  3. 删除 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/

相关文章:

android - 无法使用 Retrofit 2 和简单 XML 转换器获取 Soap 列表

android - 如何将图像添加到android中的AlertDialog?

java - 布局 - 图像不在底部且重叠

android - 在底部导航 View 中设置最初选择的项目索引/id

android - params.getBehaviour() 返回空值

android - 单击根布局时如何显示嵌套布局?

android - 无效的可绘制标签矢量

android.widget.AdapterView.fireOnSelected(AdapterView.java :892)

android-layout - ActionBar.Tablistener 已弃用

android - 如何将颜色从十六进制转换为 RGB