Snackbar没有移动放置在Fragment内部的FAB

时间:2016-01-26 13:58:55

标签: android android-layout android-fragments android-coordinatorlayout

有一个方案描述了我的应用程序的视图层次结构。

Example

还有一些XML

MainActivity

. CoordinatorLayout
  . FrameLayout <- ContentFragment inserted here
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
  . AppBarLayout
    . Toolbar

ContentFragment

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:src="@drawable/ic_plus_white_36dp"
        android:layout_gravity="bottom|end"
        android:layout_margin="15dp"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp"/>

    <TextView
        android:id="@+id/hint_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/empty_dates_list"
        android:padding="20dp"
        android:textSize="20sp"
        android:layout_gravity="center" />

</FrameLayout>

使用此代码,我制作SnackBar

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

我的问题是我在ContentFragment中fab未提升SnackBar

我还尝试将fab作为Snackbar的视图,但它没有带来结果。

2 个答案:

答案 0 :(得分:5)

您应该使用CoordinatorLayout来避免此问题,因为仅CoordinatorLayout可以正确协调由其子视图引起的布局更改。

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

答案 1 :(得分:2)

要使其发挥作用,home()应该是FloatingActionButton的孩子。你的孩子是CoordinatorLayout

相关问题