【问题标题】:Snackbar not showing up小吃店没有出现
【发布时间】:2019-02-22 03:09:33
【问题描述】:

所以,我有一个 Homepage 活动,其中包含 BottomNavigationViewFrameLayout。在点击BottomNavigationView 的标签时,我正在膨胀不同的fragments。我的代码如下:

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

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/test"
    android:layout_marginBottom="56dp">

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

其中一个片段如下:

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

现在我想在Fragment 代码中显示Snackbar 如下:

Snackbar.make(getActivity().findViewById(R.id.test), msg, Snackbar.LENGTH_LONG).show;

但我似乎犯了一些错误。应用程序没有崩溃,但Snackbar 也没有出现。我想在BottomNavigationView 上方显示Snackbar。我查看了this 并尝试实现相同的功能。请让我知道我哪里出错了。

【问题讨论】:

    标签: android android-fragments android-coordinatorlayout bottomnavigationview android-snackbar


    【解决方案1】:

    尝试设置findViewById(android.R.id.content)

    Snackbar.make(getActivity().findViewById(android.R.id.content), msg, Snackbar.LENGTH_LONG).show;
    

    【讨论】:

      【解决方案2】:

      在 HomePageActivity 中

      public CoordinatorLayout coordinatorLayout;
      

      然后在活动的onCreate中

      coordinatorLayout=findViewById(R.id.rootlayout_homepage_main);
      

      然后在你的片段中

      Snackbar.make(((HomapageActvity)getActivity()).coordinatorLayout, msg, Snackbar.LENGTH_LONG).show;
      

      【讨论】:

        【解决方案3】:
        Snackbar.make(findViewById(R.id.placeSnackBar), "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        

        【讨论】:

        • 虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,纯代码的答案没有用处。
        • 这与 OP 尝试调整/实施的代码完全相同吗?你能解释一下你的答案吗,也请使用花括号来格式化你的代码块。
        猜你喜欢
        • 2019-06-09
        • 2016-12-09
        • 1970-01-01
        • 2020-07-25
        • 2018-09-27
        • 1970-01-01
        • 1970-01-01
        • 2020-07-05
        • 2018-09-13
        相关资源
        最近更新 更多