gpt4 book ai didi

android - 转到另一个 fragment 时, float 操作按钮不会消失

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:41 26 4
gpt4 key购买 nike

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

我点击 fab button 转到另一个 fragment 但是当 fragment 移动时,fab button 不会消失!

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

这是我添加到 frameLayoutScrolling 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;
}
}

首先是屏幕截图:

enter image description here

这是转到新 fragment 时的屏幕截图:

after go to new fragment

最佳答案

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

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

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

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

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

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

关于android - 转到另一个 fragment 时, float 操作按钮不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34777008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com