在导航栏上方获取小吃店

时间:2018-09-15 08:48:46

标签: android android-coordinatorlayout bottomnavigationview

我有activity_home_page.xml,其中有一个FrameLayoutbottomNavigationView

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高于bottomNavigationView,我应该向小吃店提供哪种视图?

无法弄清楚我要去哪里。

1 个答案:

答案 0 :(得分:1)

尝试用它代替view中的SnakeBar

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

此外,删除:

android.support.design来自TextView

TextView textView = (TextView) sbView.findViewById(R.id.snackbar_text);