【发布时间】: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)
我做错了什么?
- 我检查了片段的名称是否与导航图中的 id 相同。
编辑:已解决。我的菜单中的名称与 ID 不同
【问题讨论】:
标签: android kotlin bottomnavigationview android-jetpack-navigation