作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
背景: (如果你不想知道我是怎么得到问题的,可以忽略。只是一些关于 Android Studio 模板 Activity 的问题)我一直在研究底部导航,并尝试从 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);
NavigationUI.setupActionBarWithNavController()
和
NavigationUI.setupWithNavController()
不工作。我的应用只是
闪停每当我运行它。有点奇怪,但我相信这两种方法是底部导航的官方用法。
<?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>
最佳答案
我遇到了同样的错误,我通过创建我的 解决了它自带工具栏然后将其设置为 支持操作栏如下。
最初,获取 工具栏与 findviewbyid 或使用 数据绑定(bind) 并将其分配给一个变量。在这里我将它设置为变量工具栏,然后进一步使用它。
setSupportActionBar(toolbar)
toolbar.setupWithNavController(navController, appBarConfiguration)
关于android - 底部导航 : NavigationUI. setupActionBarWithNavController() 不适用于 fragment 交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61927892/
我是一名优秀的程序员,十分优秀!