首页 > 解决方案 > 为什么底部导航片段变成黑色并且在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>

标签: javaandroidfragmentbottomnavigationview

解决方案


使用新的 JetPack Navigation,您需要将要显示的片段添加到 NavGraph。navigation您可以从目录中找到您的 NavGraph res。在您的情况下,您的 NavGraph 是bot_navigation.xml.

如果你没有看到你想要显示的片段,一旦你进入你的 NavGraph,就会有这个图标

在此处输入图像描述

单击它,您将看到可用片段和活动的列表。只需单击您想要的。

如果您的片段已经放置,请确保 id 与您的bottom_navigation_menu.xml. 此外,请确保在您的主机活动中,您设置了Navigationwith rightNavController和 with the right BottomNavigationView

让我知道它是否有帮助。


推荐阅读