底部导航视图在 Snackbar 出现时移动

3
我在CoordinatorLayout中使用BottomNavigationView,并设置app:layout_behavior=".BottomNavigationBehavior"属性以在滚动时隐藏它。问题是,当我显示Snackbar时,BottomNavigationView会向上移动以为Snackbar腾出空间,然后Snackbar出现在BottomNavigationView的后面。只有在滚动后隐藏BottomNavigationView时,才会发生这种情况。我的目标是让SnackBar出现在BottomNavigationView之上。这些是一些截图(如果需要,我可以发布一个gif):在瞬间,行为是正确的。

For a split second the behaviour is right

然后BottomNavigationView会通过过渡效果向上移动。

Then the BottomNavigationView moves up with a transition

当 Snackbar 消失后,BottomNavigationView 保留在原地直到我滚动页面。

Then when the Snackbar disappear the BottomNavigationView stays there until I scroll

这是我的代码:

activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_coordinator"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.bluemango.globe.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@android:color/white"
            android:gravity="center_vertical"
            app:titleTextAppearance="@style/MainToolbarText"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:theme="@style/ToolbarColor"
            app:layout_scrollFlags="scroll|enterAlways" >

            <ImageView
                android:id="@+id/search_iv"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_search_grey600_24dp"
                android:contentDescription="@string/search"
                android:clickable="true"
                android:focusable="true"
                android:onClick="searchArticle"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:layout_gravity="center"
                android:id="@+id/toolbar_title"
                android:textSize="20sp"
                android:textColor="@color/defSecondary"
                android:textStyle="bold"/>

        </android.support.v7.widget.Toolbar>

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:background="@android:color/white"
        app:layout_constraintBottom_toBottomOf="@id/main_coordinator"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation"
        app:itemIconTint="@drawable/bottom_nav_colors"
        app:itemTextColor="@drawable/bottom_nav_colors"
        app:layout_behavior=".BottomNavigationBehavior"/>

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

Snackbar的代码(为Fragment创建)

Snackbar snackbar = Snackbar
            .make(getActivity().findViewById(R.id.main_coordinator), getResources().getString(R.string.article_added_to_fav),
                    Snackbar.LENGTH_LONG)
            .setAction(android.R.string.cancel, new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    // onClick method.
                }
            })
            .setActionTextColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));
    snackbar.show();

谢谢帮助!

你解决过这个问题吗? - Bink
是的,请看一下我问题中的更新。 - MattButtMatt
谢谢指出这个问题 - 我猜我只是匆匆浏览了一下原帖,没有注意到。你可能想发布一个答案并接受它,以使其他人更容易看到。 - Bink
1
谢谢,我刚刚做了它。 - MattButtMatt
2个回答

1
尝试按照以下方式进行。
Snackbar snackbar = Snackbar.make(getActivity().findViewById(R.id.content_frame), ...);
snackbar.show();

没有任何变化。我更新了问题,指定了只有在滚动时底部导航视图被隐藏时才会出现这种行为。当我显示一个Snackbar时,这种情况才会发生。 - MattButtMatt

1
我修改了Behavior类,删除了任何对Snackbar的引用。然后我将Snackbars的marginBottom设置为BottomBar的高度。在Behavior类中,我添加了一个方法来显示BottomBar,所以如果我需要在滚动时隐藏BottomBar并显示Snackbar,我会使用我的新方法显示BottomBar,然后再加上额外的边距显示Snackbar。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接