【发布时间】:2020-09-07 16:37:37
【问题描述】:
背景: (如果你不想知道我是怎么得到问题的,可以忽略。只是关于 Android Studio 模板活动的一些问题)
我一直在研究底部导航,并尝试从 Android Studio 默认创建的底部导航 Activity 模板中复制 MainActivity.java 的代码。
我还从 Android Studio 找到了这个 UI 的文档。 Bottom Navigation
基本上MainActivity中调用底部导航交互的主要代码应该是这样的:
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
我的问题:
我的底部导航 xml 工作完美。但我刚刚发现NavigationUI.setupActionBarWithNavController() 和NavigationUI.setupWithNavController() 不起作用。每当我运行我的应用程序时,它就会闪烁并停止。有点奇怪,但我相信这两种方法是底部导航的官方用法。
HomePage.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:fitsSystemWindows="true"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#267A9E"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/frag_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="612dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="@id/bottom_navi"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="53dp"
android:layout_height="56dp"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="#4a90c9"
app:layout_constraintBottom_toTopOf="@+id/bottom_navi"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/plus" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案:
现在我正在使用传统方式(设置点击侦听器)来正确实现此导航。这是老式的但非常灵活,因为我有我的个性化工具栏和侧边菜单。
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
navController= Navigation.findNavController(HomeActivity.this, R.id.frag_container);
switch (item.getItemId()) {
case R.id.bottom_navi_intake:
navController.navigate(R.id.action_WeightFragment_to_IntakeFragment);
break;
case R.id.bottom_navi_weight:
navController.navigate(R.id.action_IntakeFragment_to_WeightFragment);
break;
}
return true;
}
});
R.id.action_... 是
@navigation/nav.graph.xml 中的操作 ID:
<?xml version="1.0" encoding="utf-8"?>
<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/nav_graph"
app:startDestination="@id/bottom_navi_intake">
<fragment
android:id="@+id/bottom_navi_intake"
android:name="com.example.dietdiary.IntakeFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_intake">
<action
android:id="@+id/action_IntakeFragment_to_WeightFragment"
app:destination="@id/bottom_navi_weight" />
</fragment>
<fragment
android:id="@+id/bottom_navi_weight"
android:name="com.example.dietdiary.WeightFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_weight">
<action
android:id="@+id/action_WeightFragment_to_IntakeFragment"
app:destination="@id/bottom_navi_intake" />
</fragment>
【问题讨论】:
-
ids 在 menu.xml 和 navigation_graph.xml 的片段中应该相同 stackoverflow.com/a/67643470/9315431
标签: android navigation androidx