FloatingActionButton 在 CoordinatorLayout 下不跳起来 Android

FloatingActionButton do not jump up under CoordinatorLayout Android

我正在尝试 FloatingActionButton 跳起效果(在 CoordiantorLayout 下)。触摸 Fab 时调用小吃店。然而,Fab 并没有跳起来。我哪里搞砸了?

fab.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ViewCompat.animate(fab).rotation(r).withLayer().setDuration(1000).setInterpolator(interpolator).start();
            r+=45f;
            Snackbar.make(findViewById(R.id.relative_layout), "Test", Snackbar.LENGTH_LONG)
                    .show();

            return false;
        }
    });

布局文件

    <android.support.v4.widget.DrawerLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity">



    <RelativeLayout
        android:id="@+id/relative_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FF00"
            >
        </android.support.v7.widget.Toolbar>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:layout_below="@id/toolbar"
            android:id="@+id/textView"
            android:background="#00FFFF"/>
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinator_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_add_black"

            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp"/>
        </android.support.design.widget.CoordinatorLayout>
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

嗯,你的 CoordinatorLayout 的大小是 wrap_content/wrap_content,所以它不比 FAB 大。

此外,您要传递给 Snackbar.make()View 无论如何都在 CoordinatorLayout 之外。 Snackbar 将在您的 RelativeLayout 及以上级别寻找 CoordinatorLayout,因此 Snackbar 将找不到您的 CoordinatorLayout.

我建议您更改布局,使 CoordinatorLayout 环绕 RelativeLayout

比如这个布局右下角有一个FAB,如果我给Snackbar.make()提供第一个参数ListView,FAB就会滑开:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawSelectorOnTop="false"
    />

  <android.support.design.widget.FloatingActionButton
    android:id="@+id/refresh"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="@dimen/fab_margin"
    android:layout_marginRight="@dimen/fab_margin"
    android:src="@drawable/ic_refresh_black_24dp"/>

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