Android在片段内显示Snackbar

JJS*_*ith 4 xml android android-fragments

我在片段中显示一个快餐栏时遇到了一些问题.我可以为一个活动设置一个好的但是当我尝试一个片段时它就不会出现并且没有给出错误.

片段内的小吃

Snackbar snackbar = Snackbar.make(rootView, "Snackbar test", Snackbar.LENGTH_SHORT);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();   
Run Code Online (Sandbox Code Playgroud)

片段中的rootView

    rootView = inflater.inflate(R.layout.fragment_possible_recipes, container, false);
Run Code Online (Sandbox Code Playgroud)

片段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"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="50dp"
    android:src="@drawable/no_result"
    android:visibility="gone"
    android:id="@+id/imageview"
    android:layout_gravity="center"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/filterButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="63dp"
    android:layout_marginRight="16dp"
    android:clickable="true"
    android:src="@drawable/ic_filter"
    app:backgroundTint="@color/floatingButton" />


 </android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

主要活动XML

<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout2">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

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


<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.example.rory.pocketchef.Fragments.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

sae*_*eed 12

试试这种方式

你传递View这不是一个CoordinatorLayoutSnackbar,直到它找到一个会走这棵树CoordinatorLayout或布局的根.

  rootlayout = (CoordinatorLayout) rootView.findViewById(R.id.coordinatorLayout);
Run Code Online (Sandbox Code Playgroud)

Snackbar

  Snackbar snackbar = Snackbar.make(rootlayout , "Snackbar test", Snackbar.LENGTH_SHORT);
                            View sbView = snackbar.getView();
                            TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                            textView.setTextColor(Color.YELLOW);
                            snackbar.show(); 
Run Code Online (Sandbox Code Playgroud)


flu*_*man 8

只需使用它就可以轻松完成它.

Snackbar.make(getActivity().findViewById(android.R.id.content),
           "text to show", Snackbar.LENGTH_LONG).show();
Run Code Online (Sandbox Code Playgroud)

这应该在Fragment中的任何地方都可以使用.

PS.我知道这已经过时了,但我将它留在这里以备将来参考.

  • 就像一种魅力,简单而优雅的方式来做到这一点 (2认同)