如何使用 material.BottomNavigationView 设置 Jetpack 导航 [英] How to Setup Jetpack Navigation with material.BottomNavigationView
问题描述
我无法使用 com.google.android.material.bottomnavigation.BottomNavigationView 设置导航组件,它给出了错误.
I am unable to setup Navigation component with com.google.android.material.bottomnavigation.BottomNavigationView it is giving an error.
这是我尝试使用的代码
// Setup bottom navigation view
NavigationUI.setupWithNavController(
bottom_navigation_view,
findNavController(R.id.main_nav_host_fragment)
)
当我打开 NavigationUI 类时,我注意到它只接受旧的 android.support.design.widget.BottomNavigationView 但我想使用新的 com.google.android.material.bottomnavigation.BottomNavigationView 类.有什么想法吗?
when I open the NavigationUI class, I note that it just accept the old android.support.design.widget.BottomNavigationView but I want to use the new one com.google.android.material.bottomnavigation.BottomNavigationView class. Any idea about it?
[2019 年 14 月 11 日更新]
这是一个错误,已在 Android Gradle 插件(~3.2.0-rc-01 和 3.3.0-alpha-04)中修复.您可以在以下链接中查看问题跟踪器:https://issuetracker.google.com/issues/110692942
This was a bug and was fixed at Android Gradle Plugin (~3.2.0-rc-01 and 3.3.0-alpha-04). You can see the issue tracker in the following link: https://issuetracker.google.com/issues/110692942
推荐答案
我正在使用这些库
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>
这篇关于如何使用 material.BottomNavigationView 设置 Jetpack 导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!