为什么底部导航片段变成黑色并且在安卓上无法工作?

huangapple go评论104阅读模式
英文:

Why bottom navigation fragment become black in color and not working in android?

问题

[![在这里输入图片描述][1]][1]

  [1]: https://i.stack.imgur.com/5xSH7.png

我不知道为什么片段不起作用。我正在创建底部导航。我的代码在这里。请解决我的问题。

    <?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>
英文:

为什么底部导航片段变成黑色并且在安卓上无法工作?

I don't know why fragment is not working. I am creating bottom navigation. My code is here. Please solve my problem.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:id=&quot;@+id/draw&quot;&gt;
&lt;com.google.android.material.bottomnavigation.BottomNavigationView
android:id=&quot;@+id/bot_nav_view&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;0dp&quot;
android:layout_marginEnd=&quot;0dp&quot;
android:background=&quot;?android:attr/windowBackground&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:menu=&quot;@menu/bottom_navigation_menu&quot; /&gt;

This is the fragment part. Please solve my problem. The app is not working.
This is the fragment part. Please solve my problem. The app is not working.

    &lt;fragment
android:id=&quot;@+id/nav_host_fragment&quot;
android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
app:defaultNavHost=&quot;true&quot;
app:layout_constraintBottom_toTopOf=&quot;@id/bot_nav_view&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:navGraph=&quot;@navigation/bot_navigation&quot;
/&gt;
&lt;com.google.android.material.appbar.AppBarLayout
android:id=&quot;@+id/appBarLayout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
tools:ignore=&quot;MissingConstraints&quot;&gt;
&lt;androidx.appcompat.widget.Toolbar
android:id=&quot;@+id/toolbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;?attr/actionBarSize&quot;
android:background=&quot;?attr/colorPrimary&quot;
android:fitsSystemWindows=&quot;false&quot;
app:popupTheme=&quot;@style/AppTheme.AppBarOverlay.&quot; /&gt;
&lt;/com.google.android.material.appbar.AppBarLayout&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

This is the bot_navigation.xml code. Please check it also.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/bot_navigation&quot;
tools:ignore=&quot;UnusedNavigation&quot;&gt;
&lt;fragment
android:id=&quot;@+id/navigation_feed&quot;
android:name=&quot;com.example.reducestress.ui.feed.feed&quot;
android:label=&quot;@string/title_feed&quot;
tools:layout=&quot;@layout/feed_fragment&quot; /&gt;
&lt;fragment
android:id=&quot;@+id/navigation_diary&quot;
android:name=&quot;com.example.reducestress.ui.diary.diary&quot;
android:label=&quot;@string/title_diary&quot;
tools:layout=&quot;@layout/diary_fragment&quot; /&gt;
&lt;fragment
android:id=&quot;@+id/navigation_profile&quot;
android:name=&quot;com.example.reducestress.ui.profile.profile&quot;
android:label=&quot;@string/title_profile&quot;
tools:layout=&quot;@layout/profile_fragment&quot; /&gt;
&lt;/navigation&gt;

答案1

得分: 2

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

如果您没有看到要显示的片段,在进入NavGraph后,会有这个图标

为什么底部导航片段变成黑色并且在安卓上无法工作?

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

如果您的片段已经放置好,请确保ID与您的bottom_navigation_menu.xml中的ID匹配。还要确保在您的主活动中,您正在使用正确的NavController和正确的BottomNavigationView设置Navigation

如果有帮助,请告诉我。

英文:

With the new JetPack Navigation, you need to add the fragments you want to show to your NavGraph. You can find your NavGraph under the navigation directory from the res directory. In your case, your NavGraph is bot_navigation.xml.

If you don't see the fragment you want to show, once you are inside your NavGraph, there will be this icon

为什么底部导航片段变成黑色并且在安卓上无法工作?

Click it and you will see a list of the available fragments and activities. Just click the one you want.

If your fragments are already placed, make sure the id's match with the id's in your bottom_navigation_menu.xml. Also, make sure that in your host activity you are setting up the Navigation with the right NavController and with the right BottomNavigationView.

Let me know if it helped.

答案2

得分: 1

尝试将<fragment更改为<FrameLayout,当我使用FrameLayout时,它对我起作用,但当我将其更改为<fragment时会变黑。

英文:

try to change <fragment to <FrameLayout its work for me when i use FrameLayout
and turn black when i change it to <fragment

答案3

得分: 0

你在 navigation 中忘记了将 app: startDestination = "@id/navigation_feed" 改为你想要首先启动的片段,我认为你应该移除 unusedNavigation

代码应该如下所示:

<?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>
英文:

You forgot in navigation app: startDestination = &quot;@ id / navigation_feed instead of navigation_feed put the fragment that should be launched first and i think you should remove unusedNavigation

The code that should look like:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/bot_navigation&quot;
app:startDestination=&quot;@id/navigation_feed&quot;&gt;
...
&lt;/navigation&gt;

huangapple
  • 本文由 发表于 2020年8月26日 18:05:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63595285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定