我正在使用这些库
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
// Architecture Components
def nav_version = "1.0.0-alpha02"
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
// use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
这是我的活动
NavigationUI.setupWithNavController(bottomNavView,
Navigation.findNavController(this, R.id.nav_host_fragment))
这是我的布局
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.ActivityMain">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:menu="@menu/menu_nav_drawer" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
style="@style/Widget.MaterialComponents.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/dimen_16"
android:layout_marginBottom="72dp"
app:fabSize="normal"
tools:srcCompat="@drawable/ic_add_white" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>