我想从片段和布局xml中显示snackbar就像这样
activity_main.xml中
<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"
android:id="@+id/snackbar_holder"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.NoActionBar.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_marginBottom="108dp"
android:layout_marginRight="8dp"
android:src="@drawable/ic_refresh"
app:fab_colorNormal="@color/white"
app:fab_colorPressed="@color/greyLight"
app:fab_colorRipple="@color/grey_dull"
app:fab_hideAnimation="@anim/scale_down"
app:fab_showAnimation="@anim/scale_up"
app:fab_size="mini" />
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/white_like_bg_darker"
app:layout_anchor="@+id/bnv_section_selector"
app:layout_anchorGravity="top" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bnv_section_selector"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="bottom"
app:itemBackground="@color/white"
app:itemIconTint="@drawable/bottom_navigation_selector"
app:itemTextColor="@drawable/bottom_navigation_selector"
app:menu="@menu/bottom_navigation_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<FrameLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">
</FrameLayout>
片段在FrameLayout中膨胀
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white_like_bg">
<RelativeLayout
android:id="@+id/swipe_view_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:id="@+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/strip_height_price"
android:layout_alignParentTop="true"
android:layout_marginBottom="1dp"
android:background="@color/white_like_bg"
android:scrollbars="none">
<LinearLayout
android:id="@+id/strip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" />
</HorizontalScrollView>
<View
android:id="@+id/shadow"
android:layout_width="match_parent"
android:layout_height="@dimen/element_padding"
android:layout_below="@+id/buttonLayout"
android:background="@drawable/shadown" />
<RelativeLayout
android:id="@+id/ll_filter_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="8dp"
android:layout_toLeftOf="@+id/iv_cross"
android:text="Filtered Results" />
<ImageView
android:id="@+id/iv_cross"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="?attr/selectableItemBackground"
android:padding="4dp"
android:src="@drawable/ic_close" />
</RelativeLayout>
</RelativeLayout>
<RadioGroup
android:id="@+id/rg_logistics_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/swipe_view_holder"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:visibility="gone">
<RadioButton
android:id="@+id/rb_logistics_tab_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/checkable_bg_selector"
android:button="@null"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:text="@string/iron_ore"
android:textColor="@color/checkable_text_color_selector" />
<RadioButton
android:id="@+id/rb_logistics_tab_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/checkable_bg_selector"
android:button="@null"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:text="@string/coal"
android:textColor="@color/checkable_text_color_selector" />
<RadioButton
android:id="@+id/rb_logistics_tab_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/checkable_bg_selector"
android:button="@null"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:text="@string/met_coke"
android:textColor="@color/checkable_text_color_selector" />
</RadioGroup>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/rg_logistics_selector">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top">
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/head_padding"
android:layout_marginRight="@dimen/head_padding"
android:layout_marginTop="@dimen/head_padding"
android:clipToPadding="false"
android:nestedScrollingEnabled="true"
android:paddingBottom="@dimen/row_height"
android:visibility="visible" />
</android.support.v4.widget.SwipeRefreshLayout>
<TextView
android:id="@+id/message_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true"
android:gravity="center"
android:padding="@dimen/outer_padding"
android:textColor="@color/text_grey"
android:textSize="@dimen/textSizeHighlight"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/progresslayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true"
android:visibility="gone">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
我添加了snackbar。但它没有出现。这是我用来显示小吃栏的代码
View parent = getActivity().findViewById(R.id.snackbar_holder);
Snackbar.make(parent,
"Seems you are logged out", BaseTransientBottomBar.LENGTH_LONG).show();
即使我试图在MainActivity本身展示它,但没有运气...... 我错过了什么吗?
答案 0 :(得分:0)
试试这个
CoordinatorLayout parent = (CoordinatorLayout)getActivity().findViewById(R.id.snackbar_holder)
Snackbar snackbar = Snackbar.make(parent , "Seems you are logged out", 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();
答案 1 :(得分:0)
由于您的父级布局为CoordinatorLayout
以显示小吃栏,因此您必须首先初始化CoordinatorLayout
CoordinatorLayout coordinatorlayout = (CoordinatorLayout)view.findViewById(R.id.snackbar_holder)
Snackbar.make(coordinatorlayout, "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();