我试图将我的底部 appBar 隐藏在其他片段中,因此它不可见。我尝试这样做
public void hideBottomMenu() {
appBarLayout.setVisibility(View.GONE);
bottomNavigationView.setVisibility(View.GONE);
fabAdd.hide();
}
以下是一些截图:
但它无法正常工作,在我看来,它变得不可见,但是当我滚动时,某种视图出现在屏幕底部(底部应用程序栏的可见性都消失了)。
这是我的 MainActivity 布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="55dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_scrollFlags="scroll|enterAlways"
app:menu="@menu/top_menu"
app:title="Реєстр ТТН" />
<FrameLayout
android:id="@+id/flInternetAbsent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E36363"
android:padding="2dp"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:letterSpacing="0.05"
android:text="Відсутній інтернет"
android:textColor="@color/white" />
</FrameLayout>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="20dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
app:itemIconTint="@color/bottom_nav_color"
app:itemTextColor="@color/bottom_nav_color"
app:menu="@menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add"
app:backgroundTint="@color/primary_color"
app:layout_anchor="@id/bottomAppBar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
有很多方法可以实现这一点(我不喜欢其中任何一个)。我更喜欢在片段内部设置底部栏,因为在大多数片段中我不使用它。因此对于少量片段来说这很好。
第二种方法与您的方法相同。请注意,按此顺序设置可见性非常重要(我在经过数天无望的方法后通过尝试和错误实现了这一点。):首先隐藏 FAB(使用
fab.hide()
),然后隐藏它(我这样做只是为了确保它不会再出现。使用 fab.setVisibility(View.GONE)
) ,然后使用 Invisible BottomBar (使用 bottomAppBar.setVisibility(View.GONE)
)。总结:
public void hideBottomMenu() {
fabAdd.hide();
fabAdd.setVisibility(View.GONE);
appBarLayout.setVisibility(View.GONE);
//bottomNavigationView.setVisibility(View.GONE); do not use this. it is a child of bottombar.
}
为了显示这些组件,定义:
public void showBottomMenu() {
appBarLayout.setVisibility(View.VISIBLE);
appBarLayout.performShow();
//fabAdd.setVisibility(View.VISIBLE); // I am not sure about this.
fabAdd.show();
//bottomNavigationView.setVisibility(View.GONE); do not use this. it is a child of bottombar.
}
然后在要隐藏bottomBar的片段的
onCreate
方法中,将其称为
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MainActivity) requireActivity()).hideBottomMenu();
}