简体   繁体   English

在android studio中设计时不显示片段的结构。只显示阴影。如何在设计时获取原始片段?

[英]Structure of fragment isn't shown during design in android studio.Only the shade is shown.How to get the original fragment during design?

While designing the main activity I tried to add two fragments.But the original structure of the fragment is not shown in design tab of activity_main, instead shaded regions are shown for each fragment.How to get original structure of fragments during design?在设计主活动时,我尝试添加两个片段。但是在activity_main的设计选项卡中没有显示片段的原始结构,而是为每个片段显示了阴影区域。如何在设计过程中获得片段的原始结构? enter image description here在此处输入图片说明

In your activity_main.xml make sure each of your fragment tags specify:在您的 activity_main.xml 中,确保您的每个片段标签都指定:

  • android:name with a path to your fragment android:name带有片段路径
  • tools:layout with the fragment's layout tools:layout与片段的布局

If you're using an include tag then you'll need to use something like tools:showIn=".MainActivity" ( docs ) in your fragments.如果您使用包含标签,那么您需要在片段中使用类似tools:showIn=".MainActivity" ( docs ) 的内容。

Here's an example:下面是一个例子:

activity_main.xml活动_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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM