【发布时间】:2020-08-22 11:17:01
【问题描述】:
我正在运行 Android Studio 3.6.3,我正在尝试使用作为 Android Jetpack 一部分的新导航资源。我做了Navigation Codelab 来学习如何使用这个功能。
在我的项目中,我添加了一个导航资源,Android Studio 自动添加了此功能的依赖项。然后,我创建了一个带有 NavHostFragment 的 Activity 布局。
但是,当我转到导航资源时,左侧的 HOST 部分显示为 No NavHostFragments found。
我尝试过同步 Gradle、清理和重建,但无济于事。
有趣的是,当我预览我的主 Activity 布局时,“home destination”片段通过NavHostFragment 窥视,所以看起来关系是在一个方向上建立的,而不是在另一个方向上。
如何让我的NavHostFragment 出现在导航资源中?
这是我的布局 XML:
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context="my.app.MyActivity"
>
<data>
<variable
name="viewModel"
type="my.app.MyViewModel" />
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<fragment
android:id="@+id/my_nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:navGraph="@navigation/central_navigation" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
【问题讨论】:
-
请包含您的布局 XML。
-
好点@ianhanniballake
-
好吧,今天我的 NavHost 出现在我的 Navigation XML 中。从那时到现在发生了很多事情,所以很难知道它为什么开始工作,但我没有改变上面的布局。
标签: android android-studio android-jetpack-navigation