![](/img/trans.png)
[英]Android Navigation Fragment background is black without setting
[英]Why bottom navigation fragment become black in color and not working in 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。 您可以在res
目錄的navigation
目錄下找到您的 NavGraph。 在您的情況下,您的 NavGraph 是bot_navigation.xml
。
如果你沒有看到你想要顯示的片段,一旦你進入你的 NavGraph,就會有這個圖標
單擊它,您將看到可用片段和活動的列表。 只需單擊您想要的那個。
如果您的片段已經放置,請確保 id 與您的bottom_navigation_menu.xml
的 id 匹配。 此外,確保在您的主機的活動正在設置的Navigation
與正確NavController
和正確BottomNavigationView
。
如果有幫助,請告訴我。
嘗試將 <fragment 更改為 <FrameLayout 它在我使用 FrameLayout 時對我有用,當我將其更改為 <fragment 時變黑
您在navigation
app: startDestination = "@ id / navigation_feed
忘記了app: startDestination = "@ id / navigation_feed
而不是 navigation_feed 放置應該首先啟動的片段,我認為您應該刪除未使用的導航
應如下所示的代碼:
<?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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.