【问题标题】:Navigation component with bottom nav bar not working底部导航栏的导航组件不起作用
【发布时间】:2021-05-12 12:31:47
【问题描述】:

我正在尝试使用底部导航栏设置导航组件。它正在设置,因为我得到了已设置为家庭的片段作为片段。但是,按下其他按钮并没有改变。

activity_main.xml

<fragment
            android:id="@+id/fragNavHost"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/bottom_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:layout_constraintBottom_toBottomOf="parent"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        style="@style/Widget.MaterialComponents.BottomNavigationView"
        app:menu="@menu/menu"/>

bottom_nav_graph.xml

<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"
    android:id="@+id/bottom_nav_graph.xml"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.testproject.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/profileFragment"
        android:name="com.example.testproject.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" />
    <fragment
        android:id="@+id/searchFragment"
        android:name="com.example.testproject.SearchFragment"
        android:label="fragment_search"
        tools:layout="@layout/fragment_search" />
</navigation>

并像这样在主要活动中使用它。

        // Finding the Navigation Controller
        var navController = findNavController(R.id.fragNavHost)

        // Setting Navigation Controller with the BottomNavigationView
        bottomNavView.setupWithNavController(navController)

我做错了什么?

  1. 我检查了片段的名称是否与导航图中的 id 相同。

编辑:已解决。我的菜单中的名称与 ID 不同

【问题讨论】:

    标签: android kotlin bottomnavigationview android-jetpack-navigation


    【解决方案1】:

    我在 onCreate 中执行以下代码以使其正常工作。

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController
        bottomNavigation.setupWithNavController(navController)
    

    【讨论】:

    • 谢谢你让我成功了。菜单中我的 ID 名称不同,这导致了问题。赞成帮助..:)
    【解决方案2】:

    使用导航控件时,需要使用navigation()方法导航到对应的fragment

    像这样:

    findNavController().navigate(R.id.action_inputNetInfoFragment_to_inputMasterInfoFragment)
    

    【讨论】:

    • 不是真的。我试图找到一种方法将我的导航组件添加到底部栏,而不是从一个片段导航到另一个片段。
    • 对不起,我误解了你的意思
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    相关资源
    最近更新 更多