【问题标题】:Floating action button will not disappear when going to another fragment转到另一个片段时,浮动操作按钮不会消失
【发布时间】:2016-04-19 00:52:29
【问题描述】:

我基于Scrolling Activity 构建了一个项目,遇到了一个奇怪的问题。考虑以下场景:

我点击fab按钮去另一个fragment但是当fragment移动时,fab按钮不会消失

有人知道如何解决这个问题吗?

这是我添加到frameLayout 中的Scrolling Activity 的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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="ir.apio.myapplication.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_scrolling" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end" />


    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>

content_scrolling.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="ir.apio.myapplication.ScrollingActivity"
    tools:showIn="@layout/activity_scrolling">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:text="@string/large_text" />

</android.support.v4.widget.NestedScrollView>

我的工厂 ClickListener:

fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.frame,new TestFragment())
                        .commit();
            }
        });

还有我的 TestFragment :

public static class TestFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_test,container,false);
            return view;
        }
    }

首先是屏幕截图:

这是进入新片段时的屏幕截图:

【问题讨论】:

  • 将 app:layout_anchor="@id/app_bar" 替换为 app:layout_anchor="@+id/app_bar"
  • @VVB 我做到了,但没有任何改变:(
  • 请分享 content_scrolling xml
  • @VVB 我没有改变任何东西我只是去另一个片段,但我会分享它:)
  • @LinX64 当您想创建新项目时,您是否看到 android studio 提供的示例活动?我选择其中一个称为 Scrolling Activity 然后在 FAB 上添加操作,当单击该操作时转到新片段但是当转到新片段时 FAB 仍然可见.它被放置在 Fragment 的顶部,而这不应该发生。

标签: android android-fragments material-design floating-action-button android-support-design


【解决方案1】:

您仍然看到 FAB 的原因是 elevation,它指的是屏幕上的视图深度。它会影响哪些元素在彼此上方或下方以及它们投射的阴影(例如,FAB 位于主要内容之上并投射阴影)。

默认情况下,FAB 会有一些海拔,您可以使用海拔属性覆盖它,例如app:elevation="4dp"。其他元素将处于 0dp 级别。

在您的情况下,您已将 FrameLayout 放在布局文件中的最后,我想这就是您将片段加载到的位置。这实际上并没有替换您的其他内容,而只是用 FrameLayout 的内容覆盖它。

它没有覆盖 FAB 的原因是因为 FAB 有一定的高度,而 FrameLayout 没有。因此,尽管您已将 FrameLayout 放在最后,并且通常希望它位于“顶部”,但 FAB 实际上具有更高的高度,它会覆盖它。

一个快速的解决方法是给你的浮动操作按钮app:elevation=0dp,这将使它回到与其他所有东西相同的高度,并且将适用正常规则,FrameLayout 是最后一个并且会在顶部。

实际上,仅仅放置一个大框架来覆盖之前的内容通常不是最好的做法,您可能需要考虑其他方式来构建应用程序。

【讨论】:

  • 谢谢,您的回答很有效,在这种情况下,您有什么建议可以按照您的部分回答进行操作,如果您能指导我,我真的很感激:)? '放置一个覆盖之前内容的大框架通常不是最好的做法,您可能想看看其他方式来构建应用程序'
  • 嗯,这是一个相当大的话题,所以我不能在这里详细介绍。作为一个起点,我想说如果你想用新的东西替换屏幕上的所有东西,那么你可能会使用一个新的活动,但是如果你想保留一些部分(比如保持相同的菜单),那么你可能想用一个新的片段替换 部分屏幕。这只是一个简单的例子,有很多关于何时以及如何使用片段的信息。
  • 也偶然发现了这个问题,问题是我没有在晶圆厂上设置高程,如果我对 0dp 进行任何更改...今天有什么最佳做法吗?问题是 2 岁:D 如果不是,我需要自己创建一个问题
  • 当您将高度设置为 0 时,晶圆厂确实会消失。但是,它仍然是可点击的。
【解决方案2】:

我遇到了同样的问题。我在FragmentTransaction 上调用了.hide() 方法,它对我有用。

fab.setOnClickListener {
        val fragmentManager = fragmentManager
        val fragmentTransaction = fragmentManager?.beginTransaction()
        val fragment = YourFragment()
        fragmentTransaction?.add(R.id.fragment_container, fragment)
        fragmentTransaction?.addToBackStack(null)
        fragmentTransaction?.hide(this)
        fragmentTransaction?.commit()
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多