【问题标题】:How to Setup Jetpack Navigation with material.BottomNavigationView如何使用 material.BottomNavigationView 设置 Jetpack 导航
【发布时间】:2018-12-02 23:33:51
【问题描述】:

我无法使用 com.google.android.material.bottomnavigation.BottomNavigationView 设置导航组件,它给出了错误。

这是我尝试使用的代码

 // 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 类。有什么想法吗?

[2019 年 14 月 11 日更新]

这是一个错误,已在 Android Gradle 插件(~3.2.0-rc-01 和 3.3.0-alpha-04)中修复。您可以在以下链接中查看问题跟踪器:https://issuetracker.google.com/issues/110692942

【问题讨论】:

    标签: android navigation android-studio-3.0 android-jetpack


    【解决方案1】:

    我正在使用这些库

    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>
    

    【讨论】:

    • 对我来说仍然无法正常工作。它只需要到主页,当我点击其他选项卡时,它不会到那里。
    【解决方案2】:

    请更新“android.arch.navigation:navigation-ui:1.0.0-alpha03”来解决这个问题

    NavigationUI updated

    【讨论】:

      【解决方案3】:

      查看导航文档:https://developer.android.com/topic/libraries/architecture/adding-components#navigation 截至 2018 年 6 月 29 日。

      上面写着:

      块引用 导航类已经在 androidx.navigation 包中,但目前依赖于 Support Library 27.1.1 和相关的 Arch 组件版本。未来会发布带有 AndroidX 依赖的 Navigation 版本。

      我想你现在别无选择,只能等待 Navigation 包的“未来”版本。

      目前,正如@Levi Albuquerque 所建议的,您需要使用com.android.support:design:28.0.0-alpha3
      它的工作方式应该与com.google.android.material:material:1.0.0-alpha3 相同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-25
        • 2023-04-03
        • 1970-01-01
        • 2022-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多