Android Studio 布局编辑器不显示布局

Android Studio Layout Editor doesn't show Layout

我正在尝试使用导航片段创建一个简单的应用程序界面,它通过 navController 控制的底部导航栏交换片段进出。

当我 运行 应用程序按预期工作时,但是在 activity_main.xml 文件中它没有显示我看到的布局运行 申请。这有什么原因吗?

activity 布局的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:menu="@menu/bottom_navigation_menu"
        tools:visibility="visible" />
        <!--android:background="#fff"/>-->

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="visible"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation_graph" />


</androidx.constraintlayout.widget.ConstraintLayout>

FragmentContainerView是一个占位符,可以在你做分片交易的时候一次显示一个分片;所以在设计时,Android studio 不确定哪个片段将首先托管在这个占位符中,所以它保持空白。

但是如果你想在 navigation_graph 中显示 start_destination 片段你可以使用 tools:layout 属性,这只在设计时有效,而不是 运行 时间。

假设您要在 FragmentContainerView 中首先托管的片段布局的名称是 fragment_start_destination

tools:layout="@layout/fragment_start_destination"