简体   繁体   中英

android: How to preview non-default fragment xml in Android Studio

Ok, this might be a little tricky, but I wonder if this is possible.

Let's say, I have a main activity layout -- which contains an include that includes one specific fragment -- named activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:orientation="vertical">
    <!-- some other things... -->
    <include
        android:id="@+id/mainContent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/itemBelow"
        app:layout_constraintTop_toBottomOf="@+id/itemAbove">
        <!-- load fragments here -->
    </include>
    <!-- some other things... -->
</android.support.constraint.ConstraintLayout>

and I have a fragment layout, for example: fragment_1.xml .

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
    <!-- something... -->
</android.support.constraint.ConstraintLayout>

I know in activity_main.xml , I can preview fragment_1.xml by adding tools:layout="@layout/fragment_1" in include ;

and in fragment_1.xml , I can preview the activity_main.xml by adding tools:showIn="@layout/activity_main" in the base ConstraintLayout .

But here comes the question: for re-usable layout design, there might be another fragment that shows in include , and if I follow method above -- let's say, in fragment_2.xml I write:

<android.support.constraint.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:showIn="@layout/activity_main">
    <!-- something... -->
</android.support.constraint.ConstraintLayout>

I would just see fragment_1.xml inside activity_main.xml in preview.

Is there any possible way that shows fragment_2.xml inside activity_main.xml -- when previewing fragment_2.xml in Android Studio?

Furthermore: if there's more include s inside activity_main.xml , would the method still works?

What you want is impossible (for now, anyway).

The layout preview does not execute any Android code (no Java, no Kotlin). All it does is parse the XML tree and show you what the XML says to show.

From the tools documentation :

the layout preview cannot execute the activity code that normally applies the layout

When you use tools:layout for a <fragment> tag, you're saying "show this layout instead of a gray square". When you use tools:showIn on a root layout tag, you're saying "show this other layout instead of the normal preview". There's no "intelligence" here that can determine that you're asking the preview to show your view "inside" of another layout that doesn't include it. It just blindly shows you activity_main 's layout preview (which will, in turn, blindly include fragment_1 's layout).


To see this in extremely obvious terms, imagine these two layouts:

  • activity_main.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second"/>

</LinearLayout>
  • itemview.xml
<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=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HELLO WORLD"/>

</FrameLayout>

Despite the fact that there are no <fragment> or <include> tags in activity_main.xml , the layout editor preview for itemview.xml will only show you "first" and "second". The tools:showIn attribute simply completely overrides the normal preview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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