1

我试图在 SnackBar 顶部显示我的 FloatingActionButton,但我无法像在其他屏幕上那样做到这一点。我目前的布局是这样的:

<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 时,浮动操作按钮悬停在其上方?

4

5 回答 5

2

我一直在寻找答案,最后我找到了对我有用的解决方案:

  1. FloatingActionButton 必须CoordinatorLayout内
  2. Snackbar 应该接收FloatingActionButton 作为它的 View 参数

布局:

 <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();
于 2016-01-28T20:56:43.123 回答
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();
        }
    });
于 2015-08-13T06:32:42.347 回答
1

尝试将此添加到您的 FAB:

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

并删除:

android:layout_gravity="bottom|end"
于 2015-08-13T03:03:55.057 回答
0

问题是,根据谷歌指南,您不应该在 Snackbar 上方显示 FAB,您应该滑动 FAB 并在其下方显示 Snack bar

检查此链接:

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

于 2015-07-31T11:03:09.650 回答
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();
    }
});
于 2015-07-31T11:05:03.067 回答