首页 > 解决方案 > android studio设计时不显示fragment的结构,只显示阴影。设计时如何获取原始fragment?

问题描述

在设计主要活动时,我尝试添加两个片段。但是在activity_main的设计选项卡中没有显示片段的原始结构,而是为每个片段显示了阴影区域。在设计过程中如何获得片段的原始结构? 在此处输入图像描述

标签: androidandroid-fragments

解决方案


在您的 activity_main.xml 中,确保您的每个片段标签都指定:

  • android:name有你的片段的路径
  • tools:layout与片段的布局

如果您使用的是包含标签,那么您需要在片段中使用类似tools:showIn=".MainActivity"( docs ) 的内容。

这是一个例子:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <fragment android:name="com.example.FirstFragment"
        android:id="@+id/firstFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_first" />

    <fragment android:name="com.example.SecondFragment"
        android:id="@+id/secondFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_second" />


</LinearLayout>

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FirstFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

推荐阅读