java - 为什么底部导航片段变成黑色并且在android中不起作用?
问题描述
我不知道为什么片段不起作用。我正在创建底部导航。我的代码在这里。请解决我的问题。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/draw">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bot_nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_navigation_menu" />
这是片段部分。请解决我的问题。该应用程序无法正常工作。这是片段部分。请解决我的问题。该应用程序无法正常工作。
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/bot_nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/bot_navigation"
/>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="false"
app:popupTheme="@style/AppTheme.AppBarOverlay." />
</com.google.android.material.appbar.AppBarLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是 bot_navigation.xml 代码。请也检查一下。
<?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/bot_navigation"
tools:ignore="UnusedNavigation">
<fragment
android:id="@+id/navigation_feed"
android:name="com.example.reducestress.ui.feed.feed"
android:label="@string/title_feed"
tools:layout="@layout/feed_fragment" />
<fragment
android:id="@+id/navigation_diary"
android:name="com.example.reducestress.ui.diary.diary"
android:label="@string/title_diary"
tools:layout="@layout/diary_fragment" />
<fragment
android:id="@+id/navigation_profile"
android:name="com.example.reducestress.ui.profile.profile"
android:label="@string/title_profile"
tools:layout="@layout/profile_fragment" />
</navigation>
解决方案
使用新的 JetPack Navigation,您需要将要显示的片段添加到 NavGraph。navigation
您可以从目录中找到您的 NavGraph res
。在您的情况下,您的 NavGraph 是bot_navigation.xml
.
如果你没有看到你想要显示的片段,一旦你进入你的 NavGraph,就会有这个图标
单击它,您将看到可用片段和活动的列表。只需单击您想要的。
如果您的片段已经放置,请确保 id 与您的bottom_navigation_menu.xml
. 此外,请确保在您的主机活动中,您设置了Navigation
with rightNavController
和 with the right BottomNavigationView
。
让我知道它是否有帮助。
推荐阅读
- sbt - 如何使用 Dotty 运行 JMH?
- java - 将项目导出到 zip 文件时如何更改 java 框架中的属性
- git - Git:git@gitlab.com:当我按下“同步更改”按钮时权限被拒绝(公钥)
- json - 从 MVC 中的控制器返回时出现 JavaScript 错误
- python - 为什么我在流式传输时收到“JSONDecoderError”?
- java - 在最近的 Java 版本(8+)中可以省略菱形运算符吗?
- javascript - 替代 FormData 以保持属性的顺序
- directx-11 - 像素着色器中如何使用顶点数据
- php - 为什么带有while循环的PHP Curl会产生504超时?
- javascript - “findById”方法不起作用,即使我提供了正确的对象 ID