我强烈推荐使用导航组件。
https://developer.android.com/guide/navigation/navigation-getting-started
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
创建一个菜单:res/menu/my_navigation_items.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_home"
android:title="@string/menu_home"
android:icon="@drawable/ic_home" />
<item android:id="@+id/action_dashboard"
android:title="@string/menu_dashboard"
android:icon="@drawable/ic_dashboard" />
<item android:id="@+id/action_profile"
android:title="@string/menu_profile"
android:icon="@drawable/ic_profile" />
</menu>
并使用 app:menu="@menu/my_navigation_items" 将其分配为 bottomNavigationView 菜单:
<com.google.android.material.bottomnavigation.BottomNavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schema.android.com/apk/res/res-auto"
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="start"
app:menu="@menu/my_navigation_items" />
最后最重要的是,在 androidx.fragment.app.FragmentContainerView 中使用导航图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:navGraph="@navigation/navigation_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/my_navigation_items"
app:labelVisibilityMode="labeled"/>
</LinearLayout>
此图表将自动实例化您的片段。你应该在 res/navigation/ 中创建它:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
app:startDestination="@id/home_fragment">
<fragment
android:id="@+id/home_fragment"
android:name="<yourpackage>.HomeFragment"
android:label="@string/home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/dashboard_fragment"
android:name="<yourpackage>.DashboardFragment"
android:label="@string/informations"
tools:layout="@layout/fragment_report" />
<fragment
android:id="@+id/profile_fragment"
android:name="<yourpackage>.ProfileFragment"
android:label="@string/profile"
tools:layout="@layout/profile_layout" />
</navigation>