Snackbar顶部的浮动动作按钮。怎么样?

时间:2015-07-31 10:54:10

标签: android android-support-library floating-action-button androiddesignsupport android-snackbar

我正在尝试将我的FloatingActionButton显示在SnackBar之上,但我无法像在其他屏幕上那样完成它。我目前的布局是这样的:

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:elevation="@dimen/toolbar_elevation"
            android:minHeight="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.ToolbarPopUp"
            app:theme="@style/AppTheme.Toolbar"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            style="@style/Feed.TabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="4dp"/>

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

            <android.support.v4.view.ViewPager
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                style="@style/FloatingActionButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:src="@drawable/ic_start"
                app:backgroundTint="@color/h19_green"
                app:elevation="6dp"
                app:pressedTranslationZ="12dp"/>
        </android.support.design.widget.CoordinatorLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/navdrawer"/>

</android.support.v4.widget.DrawerLayout>

我的Snackbar代码就是这个:

@Override
public void alertNoInternetConnection() {
   CoordinatorLayout coordinatorLayout =    ButterKnife.findById(getActivity(), R.id.coordinator_fab);
     Snackbar.make(coordinatorLayout, R.string.connection_offline, Snackbar.LENGTH_INDEFINITE).show();
}

我应该如何重新组织我的布局,以便当我显示Snackbar时,浮动操作按钮会悬停在它上面?

5 个答案:

答案 0 :(得分:2)

我看起来很长的答案,最后我找到了对我有用的解决方案:

  1. FloatingActionButton必须中> CoordinatorLayout
  2. Snackbar 接收 FloatingActionButton,因为它的View参数
  3. 布局:

     <android.support.design.widget.CoordinatorLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fabAdd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|end"
                    android:src="@mipmap/ic_create_white_48dp"
                    />
     </android.support.design.widget.CoordinatorLayout>
    

    代码:

    FloatingActionButton fabAdd = (FloatingActionButton)findViewById(R.id.fabAdd);
    Snackbar.make(fabAdd, "Record was removed.", Snackbar.LENGTH_SHORT).show();
    

答案 1 :(得分:1)

你的布局实现是错误的,这是正确的实现(例如):

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
... >

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

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_done" />

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

<include layout="@layout/navdrawer"/>

</android.support.v4.widget.DrawerLayout>

然后,正确显示Snackbar:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

答案 2 :(得分:0)

问题在于,根据Google指南,您不应在Snackbar上方显示FAB,您应该刷FAB并在其下方显示小吃栏

检查此链接:

http://www.google.com/design/spec/components/snackbars-toasts.html#snackbars-toasts-usage

答案 3 :(得分:0)

这对我有用。

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

fabBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Snackbar.make(rootLayout, "Hello. I am Snackbar!", 
Snackbar.LENGTH_SHORT)
                .setAction("Undo", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                })
                .show();
    }
});

答案 4 :(得分:0)

尝试将此添加到您的FAB:

app:layout_anchor="@id/coordinator_fab"
app:layout_anchorGravity="bottom|right"

并删除:

android:layout_gravity="bottom|end"