android – Snackbar没有移动放置在Fragment中的FAB

前端之家收集整理的这篇文章主要介绍了android – Snackbar没有移动放置在Fragment中的FAB前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有一个方案描述了我的应用程序的视图层次结构.

还有一些XML

主要活动

  1. . CoordinatorLayout
  2. . FrameLayout <- ContentFragment inserted here
  3. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  4. . AppBarLayout
  5. . Toolbar

ContentFragment

  1. <FrameLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6.  
  7. <android.support.v7.widget.RecyclerView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:scrollbars="vertical"/>
  11.  
  12. <android.support.design.widget.FloatingActionButton
  13. android:id="@+id/fab"
  14. android:layout_width="56dp"
  15. android:layout_height="56dp"
  16. android:src="@drawable/ic_plus_white_36dp"
  17. android:layout_gravity="bottom|end"
  18. android:layout_margin="15dp"
  19. app:elevation="6dp"
  20. app:pressedTranslationZ="12dp"/>
  21.  
  22. <TextView
  23. android:id="@+id/hint_text"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:gravity="center"
  27. android:text="@string/empty_dates_list"
  28. android:padding="20dp"
  29. android:textSize="20sp"
  30. android:layout_gravity="center" />
  31.  
  32. </FrameLayout>

使用此代码我制作SnackBar:

  1. Snackbar snackbar = Snackbar.make(
  2. mainActivity.getCoordinatorLayout(),R.string.date_removed,Snackbar.LENGTH_LONG);

我的问题是,我在ContentFragment中制作的SnackBar没有提升fab

我也试过点fab作为Snackbar的视图,但它没有带来结果.

解决方法

您应该使用CoordinatorLayout来避免此问题,因为CoordinatorLayout单独正确地协调由其子视图导致的布局更改.

解决您的问题,只需将FloatingActionButton从片段布局移动到MainActivity布局即可.或者,如果您打算在片段本身中使用FloatingActionButton,则将片段的父视图设置为CoordinatorLayout

猜你在找的Android相关文章