我基于Scrolling Activity
构建了一个项目,但遇到了一个奇怪的问题。请考虑以下情形:
我单击fab按钮转到另一个fragment
,但是当片段移位时,fab按钮不会消失!
有人知道如何解决此问题吗?
这是我添加到Scrolling Activity
的XML frameLayout
:
<?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;
}
}
这里是屏幕截图:
进入新片段时的屏幕截图:
您仍然看到FAB的原因是仰角,它是指屏幕上的视图深度。它影响哪些元素在彼此之上或之下,以及它们所投射的阴影(例如,FAB位于主要内容的顶部并投射阴影)。
默认情况下,FAB将具有某些标高,您可以使用标高属性(例如app:elevation="4dp"
)覆盖标高。其他元素将处于0dp级别。
在您的情况下,您已将FrameLayout放在布局文件的最后,我想这就是您要将片段加载到的位置。这实际上并不能代替您的其他内容,而只是用FrameLayout的内容覆盖它。
之所以没有覆盖FAB,是因为FAB具有一定的高度,而FrameLayout没有。因此,尽管您已将FrameLayout放在最后,并且通常希望它位于“顶部”,但FAB实际上具有更高的海拔高度,可以将其覆盖。
一种快速解决方案是将您的浮动操作按钮app:elevation=0dp
放回与其他所有位置相同的海拔高度,并且将应用常规规则,FrameLayout位于最后并且位于顶部。
实际上,通常仅放一个大框架来覆盖先前的内容通常不是最好的选择,您可能会想了解构建应用程序的其他方式。
我有同样的问题。我在.hide()
上调用了FragmentTransaction
方法,它对我有用。
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()
}