简体   繁体   English

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

[英]Floating Action Button on top of Snackbar. How?

I'm trying to show my FloatingActionButton on top of the SnackBar but I just can't make it as I have done on other screens. 我试图在SnackBar上显示我的FloatingActionButton,但我不能像在其他屏幕上那样做。 My current layout is this one: 我目前的布局是这样的:

<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>

And my Snackbar code is this one: 我的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();
}

How should I reorganize my layout so that when I display the Snackbar, the Floating Action Button hovers above it? 我应该如何重新组织我的布局,这样当我显示Snackbar时,浮动动作按钮会在它上面盘旋?

I was looking quite long for the answer, and finally I've found the solution which worked for me: 我看起来很长的答案,最后我找到了对我有用的解决方案:

  1. FloatingActionButton must be inside CoordinatorLayout FloatingActionButton必须 CoordinatorLayout中
  2. Snackbar should receive FloatingActionButton as it's View argument Snackbar应该接收 FloatingActionButton作为View参数

Layout: 布局:

 <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>

Code: 码:

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

Your layout implementation is wrong, here's the right implementation (for example): 您的布局实现是错误的,这是正确的实现(例如):

<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>

And then, to show the Snackbar correctly: 然后,正确显示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();
        }
    });

The problem is that according to the google guidelines you should not display the FAB above the Snackbar you should swipe the FAB and display the Snack bar below it 问题是,根据谷歌指南,你不应该在Snackbar上方显示FAB,你应该刷FAB并在它下面显示小吃栏

check this link : 检查此链接:

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

this worked for me. 这对我有用。

<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();
    }
});

Try adding this to your FAB: 尝试将此添加到您的FAB:

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

and remove: 并删除:

android:layout_gravity="bottom|end"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 浮动操作按钮不会停留在SnackBar的顶部 - Floating Action Button won't stay on top of SnackBar 浮动动作按钮+小吃栏的容器 - Container for Floating Action Button + Snackbar Snackbar在按下动作按钮时隐藏了浮动动作 - Snackbar hides the Floating Action on pressing the action button on it 带有多行的浮动操作按钮 Snackbar - Floating Action Button Snackbar with multi line 浮动动作按钮在第二个Snackbar下消失 - Floating Action Button diasappears under SECOND Snackbar 如何在滚动视图顶部添加浮动操作按钮 - How to add floating action button on top of scrollview 如果在 CoordinatorLayout 中设置了 anchorView,则浮动操作按钮不会随 Snackbar 一起浮动 - Floating Action Button Not Floating With Snackbar If anchorView set within CoordinatorLayout Snackbar 通过片段导航重叠浮动操作按钮 - Snackbar Overlapping Floating Action Button Through Fragment Navigation Android底部工作表,带有带有Snackbar问题的浮动操作按钮 - Android Bottom Sheet with Floating Action Button with Snackbar Issue 上下移动浮动操作按钮以避免被快餐栏阻挡 - Moving Floating Action Button up and down to avoid getting blocked by a snackbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM