导航预览在 Android Studio 3.2 预览版中不可用

Navigation Preview unavailable in Android Studio 3.2 Preview

我有兴趣尝试 Android Studio 中显示的导航图。但是我导入 google sample

后预览不可用

我使用了 Android Studio 3.2 Preview Canary 16

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:startDestination="@+id/launcher_home">

    <fragment
        android:id="@+id/launcher_home"
        android:name="com.android.samples.arch.componentsbasicsample.StartFragment"
        android:label="Home">

        <action
            android:id="@+id/end_action"
            app:destination="@id/end_dest" />

    </fragment>

    <fragment
        android:id="@+id/end_dest"
        android:name="com.android.samples.arch.componentsbasicsample.EndFragment"
        android:label="End"
        >

    </fragment>
</navigation>

2018 年 10 月 6 日更新:

即使我重新构建项目也不起作用。但如果添加新屏幕,它会在预览模式下显示新屏幕

您应该单击导航编辑器中的 "text" 选项卡(导航图的 xml 文件),然后添加:

tools:layout="@layout/layout_name"

在目标元素内。

应该是这样的:

<fragment
    android:id="@+id/someFragment"
    android:name="com.freesoulapps.navigationtest.fragments.SomeFragment"
    android:label="Some Fragment"
    tools:layout="@layout/layout_name">
</fragment>

还有另一种方法可以在导航中进行预览 xml。 首先进入你的 xml 片段添加

tools:context="com.packagename.nameFragment"

就是这样

如果您进入导航文件,您可以在选择和导航编辑器中看到预览

如果你看一下代码是自动写入的

tools:layout="@layout/layout_name"

对我来说,在导航编辑器中添加片段之前进行预览更符合逻辑。 可能有在布局

中自动添加 tools:context 的方法

自动完成不建议 tools:context 片段只建议 tools:context Activity 主机所以你需要写片段的名字...如果有人有这个技巧

了解有关 tools:context 的更多信息: enter link description here

只需添加 tools:layout="片段名称" 对于预览不可见的每个片段。 示例:-

<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/nav_graph"
    app:startDestination="@id/startFragment">

    <fragment
        android:id="@+id/pickupFragment"
        android:name="com.example.cup_cake.PickupFragment"
        android:label="fragment_pickup"
        tools:layout="@layout/fragment_pickup" />

</navigation>