转到另一个片段时,浮动操作按钮不会消失
Floating action button will not disappear when going to another fragment
我基于Scrolling Activity
构建了一个项目,遇到了一个奇怪的问题。考虑以下情形:
我点击 fab 按钮 转到另一个 fragment
但是当片段移动时,fab 按钮不会消失!
有人知道如何解决这个问题吗?
这是我添加到 frameLayout
中的 XML Scrolling Activity
:
<?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;
}
}
首先是screen-shot:
转到新片段时screen-shot这里是:
您仍然看到 FAB 的原因是 高度,它指的是屏幕上的视图深度。它会影响哪些元素在彼此上方或下方以及它们投射的阴影(例如,FAB 位于主要内容之上并投射阴影)。
默认情况下,FAB 会有一些高度,您可以使用高度属性覆盖它,例如 app:elevation="4dp"
。其他元素将处于 0dp 级别。
在您的情况下,您已将 FrameLayout 放在布局文件的最后,我认为这就是您将片段加载到的位置。这实际上并不是替换你的其他内容,而只是用你的 FrameLayout 的内容覆盖它。
它没有覆盖 FAB 的原因是因为 FAB 有一些高度而 FrameLayout 没有。因此,尽管您将 FrameLayout 放在最后并且通常期望它是 "on top",但 FAB 实际上具有更高的高度来覆盖它。
一个快速的解决办法,就是给你的浮动操作按钮 app:elevation=0dp
,它会把它放回与其他所有东西相同的高度水平,并且会应用正常的规则,FrameLayout 是最后一个并且会打开顶.
实际上,仅仅放置一个大框架来覆盖之前的内容通常不是最好的做法,您可能会想看看其他构建应用程序的方法。
我遇到了同样的问题。我在 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()
}
我基于Scrolling Activity
构建了一个项目,遇到了一个奇怪的问题。考虑以下情形:
我点击 fab 按钮 转到另一个 fragment
但是当片段移动时,fab 按钮不会消失!
有人知道如何解决这个问题吗?
这是我添加到 frameLayout
中的 XML Scrolling Activity
:
<?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;
}
}
首先是screen-shot:
转到新片段时screen-shot这里是:
您仍然看到 FAB 的原因是 高度,它指的是屏幕上的视图深度。它会影响哪些元素在彼此上方或下方以及它们投射的阴影(例如,FAB 位于主要内容之上并投射阴影)。
默认情况下,FAB 会有一些高度,您可以使用高度属性覆盖它,例如 app:elevation="4dp"
。其他元素将处于 0dp 级别。
在您的情况下,您已将 FrameLayout 放在布局文件的最后,我认为这就是您将片段加载到的位置。这实际上并不是替换你的其他内容,而只是用你的 FrameLayout 的内容覆盖它。
它没有覆盖 FAB 的原因是因为 FAB 有一些高度而 FrameLayout 没有。因此,尽管您将 FrameLayout 放在最后并且通常期望它是 "on top",但 FAB 实际上具有更高的高度来覆盖它。
一个快速的解决办法,就是给你的浮动操作按钮 app:elevation=0dp
,它会把它放回与其他所有东西相同的高度水平,并且会应用正常的规则,FrameLayout 是最后一个并且会打开顶.
实际上,仅仅放置一个大框架来覆盖之前的内容通常不是最好的做法,您可能会想看看其他构建应用程序的方法。
我遇到了同样的问题。我在 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()
}