【问题标题】:Navigation Preview unavailable in Android Studio 3.2 PreviewAndroid Studio 3.2 Preview 中的导航预览不可用
【发布时间】:2018-11-19 10:30:56
【问题描述】:

我有兴趣尝试 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 日更新:

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

【问题讨论】:

    标签: android android-architecture-components android-architecture-navigation android-studio-3.2


    【解决方案1】:

    只需添加 工具:布局=“片段名称” 到预览不可见的每个片段。 示例:-

    <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>
    

    【讨论】:

      【解决方案2】:

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

      tools:context="com.packagename.nameFragment"
      

      就是这样

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

      如果你看代码是自动写的

      tools:layout="@layout/layout_name"
      

      对我来说,在导航编辑器中添加片段之前进行预览更符合逻辑。 可能有自动添加工具的方法:布局中的上下文

      自动补全不建议工具:上下文片段仅建议工具:上下文活动主机,因此您需要编写片段的名称...如果有人对此有技巧

      了解有关工具的更多信息:上下文: enter link description here

      【讨论】:

      • 在 ViewModel 使用片段的情况下它是否适合你,所以首先在 xml 中你有
      【解决方案3】:

      您应该在导航编辑器(导航图的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>
      

      【讨论】:

      • 好的。此解决方案有效。但我对这一说法有一个疑问。如何使用默认的片段布局而不是设计时布局?
      • 导航编辑器不知道您在片段的“onCreateView”方法中放置的布局。
      • 不正确,即使你用Fragment一创建空白Activity,设计面板仍然在activity.xml中显示片段布局,没有设置设计时间属性。
      • 我不明白,你在哪里创建空白活动?在导航编辑器里面?
      猜你喜欢
      • 1970-01-01
      • 2019-09-10
      • 2018-09-29
      • 2019-01-22
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多