获取导航栏上方的Snackbar

问题描述 投票:0回答:1

我有activity_home_page.xmlFrameLayoutbottomNavigationView

activity_home_page.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.HomePage">

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

    <FrameLayout
        android:id="@+id/container_Homepage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
   app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/nv_Homepage"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="bottom"
    android:background="@color/white"
    android:foregroundGravity="fill"
    app:elevation="8dp"
    app:itemIconSize="20dp"
    app:itemIconTint="@color/bottom_nav_custom"
    app:itemTextColor="@color/bottom_nav_custom"
    app:labelVisibilityMode="labeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_insetEdge="bottom"
    app:menu="@menu/homepage_navigation_menu" />
</android.support.design.widget.CoordinatorLayout>  

bottomNavigationBar有多个标签。单击选项卡,我在上面显示的活动中在id container_Homepage的fragments中膨胀其中一个FrameLayout。其中一个片段如下:

fragment_homepage_home.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/rootlayout_homepage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="56dp"
tools:context=".activity.HomePage">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="@dimen/fab_margin"
    app:backgroundTint="#FF31ACF2"
    app:elevation="10dp"
    app:fabSize="normal"
    app:srcCompat="@drawable/ic_call" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:scrollbars="vertical" />

<RelativeLayout
    android:id="@+id/loadingView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.5"
    android:background="#000000"
    android:visibility="gone">

    <ProgressBar
        android:id="@+id/pbLoading"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:visibility="gone" />
</RelativeLayout>

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

我已经制作了通用的方法来显示snackbar传递视图和消息。方法如下:

public static void showSnack(String msg, View view) {
    final Snackbar snackbar = Snackbar.make(view, msg, Snackbar.LENGTH_LONG);

    View sbView = snackbar.getView();
    TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
    textView.setTextColor(Color.WHITE);
    snackbar.setAction("Dismiss", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            snackbar.dismiss();
        }
    });
    snackbar.setActionTextColor(Color.WHITE);
    snackbar.show();
}

我尝试实施this但仍然得到相同。我想在snackbar上面展示bottomNavigationView,但我得到了

"java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view."

当我从rootlayout_homepage传递fragment_homepage_home作为父视图时出错。

我应该向snackbar提供哪种观点让snackbar高于bottomNavigationView

无法弄清楚我哪里出错了。

android android-coordinatorlayout bottomnavigationview
1个回答
1
投票

尝试在view中使用此代替SnakeBar

final Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), msg, ... //And the rest..

另外,删除:

来自android.support.designTextView

TextView textView = (TextView) sbView.findViewById(R.id.snackbar_text);
© www.soinside.com 2019 - 2024. All rights reserved.