【发布时间】:2019-07-23 21:06:57
【问题描述】:
我正在使用以下带有fragment 和BottomNavigationView 的活动布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/navigation_bottom"
app:defaultNavHost="true"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottom_navigation_view"
app:menu="@menu/bottom_navigation_menu"/>
</LinearLayout>
并在我的导航布局中定义了三个片段:
<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/navigation_bottom"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="fragments.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" />
//The other two fragments
</navigation>
在我的活动中,我使用以下代码:
navController = Navigation.findNavController(this, R.id.fragment);
BottomNavigationView bnw = findViewById(R.id.bottom_navigation_view);
NavigationUI.setupWithNavController(bnw, navController);
NavigationUI.setupActionBarWithNavController(this, navController);
这是我的onSupportNavigateUp 方法:
@Override
public boolean onSupportNavigateUp() {
return Navigation.findNavController(this, R.id.fragment).navigateUp();
}
但是当我按下第二个图标(片段)时,什么也没有发生。如何解决这个问题?谢谢!
【问题讨论】:
标签: android bottomnavigationview androidx android-jetpack