Android Snackbar没有出现
因此,我有一个Android Snackbar没有出现,android,android-fragments,android-coordinatorlayout,bottomnavigationview,android-snackbar,Android,Android Fragments,Android Coordinatorlayout,Bottomnavigationview,Android Snackbar,因此,我有一个主页活动,其中包含底部导航视图和框架布局。在单击BottomNavigationView的选项卡时,我正在膨胀不同的片段。我的代码如下: Snackbar.make(getActivity().findViewById(R.id.test), msg, Snackbar.LENGTH_LONG).show; activity\u homepage.xml <?xml version="1.0" encoding="utf-8"?> <android.suppor
主页
活动,其中包含底部导航视图
和框架布局
。在单击BottomNavigationView
的选项卡时,我正在膨胀不同的片段。我的代码如下:
Snackbar.make(getActivity().findViewById(R.id.test), msg, Snackbar.LENGTH_LONG).show;
activity\u 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>
但我似乎犯了一些错误。应用程序没有崩溃,但是Snackbar
也没有显示。我想在BottomNavigationView
上方显示Snackbar
。我看了一下,并试图实现相同的功能。请告诉我哪里出错。在主页活动中
public CoordinatorLayout coordinatorLayout;
然后在创建活动时
coordinatorLayout=findViewById(R.id.rootlayout_homepage_main);
然后在你的碎片里
Snackbar.make(((HomapageActvity)getActivity()).coordinatorLayout, msg, Snackbar.LENGTH_LONG).show;
尝试设置findViewById(android.R.id.content)
Snackbar.make(getActivity().findViewById(android.R.id.content), msg, Snackbar.LENGTH_LONG).show;
虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,仅代码的答案没有用处。这与OP试图调整/实现的代码完全相同?你能解释一下你的答案吗?也请用大括号格式化你的代码块。
Snackbar.make(getActivity().findViewById(android.R.id.content), msg, Snackbar.LENGTH_LONG).show;
Snackbar.make(findViewById(R.id.placeSnackBar), "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();