如何解决为什么底部导航片段会变成黑色并且无法在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导航,您需要将要显示的片段添加到NavGraph中。您可以从navigation
目录的res
目录下找到NavGraph。就您而言,您的NavGraph是bot_navigation.xml
。
如果您没有看到要显示的片段,则一旦您进入NavGraph,就会出现此图标
单击它,您将看到可用片段和活动的列表。只需单击所需的那个。
如果已经放置了片段,请确保ID与bottom_navigation_menu.xml
中的ID匹配。另外,请确保在主持人活动中使用正确的Navigation
和正确的NavController
来设置BottomNavigationView
。
让我知道是否有帮助。
,尝试将
您忘记了 navigation
app: startDestination = "@ id / navigation_feed
,而不是Navigation_feed放了应该首先启动的片段,我认为您应该删除未使用的Navigation >
代码应类似于:
<?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"
app:startDestination="@id/navigation_feed">
...
</navigation>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。