【问题标题】:Android Studio Layout Editor doesn't show LayoutAndroid Studio 布局编辑器不显示布局
【发布时间】:2021-05-03 20:57:32
【问题描述】:

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

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

活动布局的代码如下:

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

【问题讨论】:

  • 也许包括它的样子?
  • 当然不一样,在编辑器上你不会看到加载的片段,就像你运行应用程序时看到的那样。

标签: android android-studio layout graphical-layout-editor


【解决方案1】:

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

但如果您想在navigation_graph 中显示 start_destination 片段,您可以使用tools:layout 属性,这仅适用于设计时,而不适用于运行时。

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

tools:layout="@layout/fragment_start_destination"

【讨论】:

  • Dam @Zain 你怎么总是打我一拳。
猜你喜欢
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 2016-04-02
  • 2017-07-30
  • 1970-01-01
相关资源
最近更新 更多