代码之家  ›  专栏  ›  技术社区  ›  Pedro Massango

如何使用material.BottomNavigationView设置Jetpack导航

  •  9
  • Pedro Massango  · 技术社区  · 8 年前

    com.google.android.material.bottomnavigation.BottomNavigationView网站

    enter image description here

    这是我想用的代码

     // Setup bottom navigation view
        NavigationUI.setupWithNavController(
                bottom_navigation_view,
                findNavController(R.id.main_nav_host_fragment)
        )
    

    当我打开 同学们,我注意到它只接受旧的 android.support.design.widget.BottomNavigationView 但我想用新的 com.google.android.material.bottomnavigation.BottomNavigationView网站 上课。你知道吗?

    [更新日期:2019年11月14日]

    https://issuetracker.google.com/issues/110692942

    3 回复  |  直到 6 年前
        1
  •  5
  •   Summer-Yang    8 年前

    我在用这些图书馆

    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
  •  1
  •   Nantawut Tantithanakornkul    8 年前

    请更新“android.arch.navigation:navigation-ui:1.0.0-alpha03”以解决此问题

    NavigationUI updated

        3
  •  0
  •   Lucas S.    8 年前

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

    上面写着:

    块引用

    我想现在你别无选择,只能等待“未来”发布的导航包。

    目前,正如@Levi Albuquerque所建议的,您需要使用 com.android.support:design:28.0.0-alpha3 .
    com.google.android.material:material:1.0.0-alpha3 做。