我根据android studio建议的预建活动(pre-buildactivity)构建了一个项目,但遇到了一个奇怪的问题。考虑以下场景:
我点击了fab按钮转到另一个Scrolling Activity但是当片段改变时,fab按钮并没有消失!
有人知道怎么解决这个问题吗?
这是我添加到fragment中的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>

frameLayout
    <?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>

我的Fab ClickListener:
fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

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

还有我的测试片段:
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;
        }
    }

这是在进入新片段前的屏幕截图
android - 转到另一个片段时 float 操作按钮问题-LMLPHP
这是新片段的截图
android - 转到另一个片段时 float 操作按钮问题-LMLPHP

最佳答案

您仍然看到fab的原因是立面,它指的是屏幕上的视图深度。它会影响哪些元素在彼此之上或之下以及它们投射的阴影(例如,fab位于主内容的顶部并投射阴影)。
默认情况下,fab将具有一些高程,您可以使用“高程”属性覆盖这些高程,例如app:elevation="4dp"。其他元素将处于0dp级别。
在您的例子中,您已经将framelayout放在布局文件的最后一个位置,我认为这是您将片段加载到的位置。这样做并不是实际替换其他内容,而是用framelayout的内容覆盖它。
它不包含fab的原因是fab有一些标高,framelayout没有。因此,尽管您已经将framelayout放在最后,并且通常希望它是“在顶部”,但fab实际上有一个更高的标高,这会覆盖它。
一个快速的解决方案是,给你的浮动操作按钮app:elevation=0dp,它将把它放回与其他操作相同的高度,并应用常规规则,framelayout是最后一个,将位于顶部。
实际上,仅仅放一个大框架覆盖之前的内容通常不是最好的做法,你会想看看其他方式来构建应用程序。

08-18 12:06
查看更多