Android 如何查看多个活动

Android 如何查看多个活动,android,android-activity,view,bottomnavigationview,Android,Android Activity,View,Bottomnavigationview,如何在多个活动中使用BottomNavigationView,但不在其中声明它 我在main_activity.xml中声明了我的底部导航视图,并在MainActivity类中处理单击逻辑,但由于我想使用底部导航视图在3个活动(地图、朋友、聊天)之间切换,我必须在3个活动上有3个底部导航视图,但由于这是性能低效的,我想在一个类和一个布局中处理所有的点击事件和视图的初始化 (MainActivity类和main_activity.xml) 我能做什么?或者有更好的方法吗?您应该使用片段而不是活动。

如何在多个活动中使用BottomNavigationView,但不在其中声明它

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


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

您应该使用片段而不是活动。只有1个活动,即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>

从活动中,在FrameLayout中添加片段。您的BottomNavigationView将保持不变。只需单击NavigationView选项卡替换此框架布局中的片段