如何查看多个活动

How to have a view over several activities

如何在多个活动中使用我的 BottomNavigationView 但不在那里声明它?

我在我的 main_activity.xml 中声明了我的底部导航视图并在 MainActivity class 中处理了点击逻辑,但是因为我想使用底部导航视图在 3 个活动(地图、朋友、聊天)之间切换我必须在三个活动上有 3 个底部导航视图,但由于性能效率低下,我想在一个 class 和一个布局中处理所有点击事件和视图初始化 (MainActivity class 和 main_activity.xml)。

我能做什么?或者有更好的方法吗?

您应该使用片段而不是活动。只有 1 activity 即 MainActivity,在 BottomNavigationView 之上添加 BottomNavigationView 和片段。然后单击 BottomNavigationView,替换片段。 你的 XML 会是这样的,

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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">

<View
    android:id="@+id/bottom_nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"/>

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_above="@id/bottom_nav_view"></FrameLayout>

根据您的 activity,在 FrameLayout 中添加片段。您的 BottomNavigationView 将保持不变。只需在单击 NavigationView 选项卡时替换此 FrameLayout 中的片段。