英文:
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.
<?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" />
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.
<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>
This is the bot_navigation.xml code. Please check it also.
<?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>
答案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 = "@ 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:
<?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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论