【问题标题】:FloatingActionButton do not jump up under CoordinatorLayout Androidandroid下CoordinatorLayout下FloatingActionButton不跳起来
【发布时间】:2015-08-02 21:32:00
【问题描述】:

我正在尝试 FloatingActionButton 跳跃效果(在 CoordiantorLayout 下)。当 Fab 被触摸时,snackbar 会被调用。然而 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>

【问题讨论】:

    标签: android floating-action-button android-coordinatorlayout


    【解决方案1】:

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

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

    我建议您更改布局,以便 CoordinatorLayout 环绕您的 RelativeLayout

    例如,这个布局在右下角有一个FAB,如果我将ListView作为第一个参数提供给Snackbar.make(),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>
    

    【讨论】:

      猜你喜欢
      • 2020-12-07
      • 2020-11-16
      • 2018-10-21
      • 2015-11-13
      • 2015-12-17
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 2015-08-21
      相关资源
      最近更新 更多