android - 显示在 Fragment Fab 按钮顶部的 Snackbar

标签 android android-fragments android-coordinatorlayout android-snackbar

我在 Textview 和 viewpager 下有 Activity 。在 View 寻呼机的一个 fragment 中,我有一个很棒的按钮。当我在菜单上单击 Activity 时调用 Snackbar 时,Snackbar 会越过 fab 按钮。一切正常(fab 按钮被推到 snackbar 上方),如果 fab 按钮处于 Activity 状态,但我需要它 fragment 。请让我知道我哪里做错了

fragment 布局

<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/coordLayoutComments"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/mdtp_white"
   >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvComments"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabComments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="@dimen/btn_fab_margins"
        android:layout_marginRight="@dimen/btn_fab_margins"
        android:elevation="6dp"
        android:src="@drawable/ic_comment_24_5"
        app:borderWidth="0dp"
        app:fabSize="mini"
        app:pressedTranslationZ="12dp" />

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

Activity 布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="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:orientation="vertical"> <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <include
                android:id="@+id/newtoolbar"
                layout="@layout/toolbar" />

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:id="@+id/cv"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                card_view:cardCornerRadius="5dp"
                card_view:cardElevation="2dp"
                card_view:contentPadding="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical">


                    <TextView
                        android:id="@+id/tvTitle"
                        fontPath="fonts/SourceSansPro-Regular.otf"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:ellipsize="end"                        
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/tvDesc"
                        fontPath="fonts/Roboto-Italic.ttf"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="5dp"
                        android:gravity="center_horizontal"                                         
                        android:textSize="15sp"
                        android:textStyle="normal|italic"
                        tools:ignore="MissingPrefix" />
                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/newtoolbar"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:scrollbars="horizontal" />

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


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

</RelativeLayout>

最佳答案

在您的 xml 布局中,有两个 CoordinatorLayout。一个在 Activity Layout 中,另一个在 Fragment Layout 中。现在 Snackbar.make() 函数的第一个参数是将放置此 Snackbar 的父 Coordinator Layout,并且此 Coordinator Layout 的所有元素都将受到 Sanckbar 的运动/ View 变化的影响。尝试使用 Fragment Layout 的 Coordinator Layout 作为 Snackbar 的父级。

关于android - 显示在 Fragment Fab 按钮顶部的 Snackbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33068652/

相关文章:

java - Android Java HttpUrlConnection 卡住

android - 我的 findViewById() 返回 null,但它不应该

android - 带有 appbar_scrolling_behaviour 的 appbar 下方的 BottomSheet

android - 使用 CoordinatorLayout 和 AppBarLayout 查看动画变得疯狂(有时)

java - Java 中对象的链式调用和继承

android - 如何将 Ormlite 与 LoaderManager 一起使用

java - Android:与工作线程通信发送消息

android - 在 Android Fragment 中保存简单变量

java - 如何添加列表 Activity 而不是 fragment ?

android - 如何修复 Coordinator Layout 中的布局?