Android Studio:如何预览非默认片段XML?

4

好的,这可能有点棘手,但我想知道这是否可行。

假设我有一个主活动布局 - 它包含一个include,其中包括一个特定的片段 - 名称为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>

我有一个fragment布局,例如: 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>

我知道在activity_main.xml中,如果在include中添加tools:layout="@layout/fragment_1",就可以预览fragment_1.xml

而在fragment_1.xml中,如果在基础的ConstraintLayout中添加tools:showIn="@layout/activity_main",就可以预览activity_main.xml

但是问题来了:对于可重用的布局设计,可能会有另一个片段显示在include中,如果我按照上述方法--比如,在fragment_2.xml中编写:

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

我希望能在预览中仅看到activity_main.xml中的fragment_1.xml

是否有可能在Android Studio中预览fragment_2.xml时,将其显示在activity_main.xml中?

此外,如果activity_main.xml中有更多的include,该方法是否仍然适用?

1个回答

0

现在来说,你想要的是不可能的。

布局预览不执行任何Android代码(没有Java,没有Kotlin)。它所做的只是解析XML树并显示XML所要显示的内容。

从{{link1:tools文档}}中得知:

布局预览无法执行通常应用布局的活动代码

当您在<fragment>标记上使用tools:layout时,您是在说“显示此布局而不是灰色正方形”。当您在根布局标记上使用tools:showIn时,您是在说“显示此其他布局而不是正常预览”。这里没有“智能”,不能确定您要求预览在不包括它的另一个布局中显示您的视图。它只是盲目地显示activity_main的布局预览(这将反过来盲目地包括fragment_1的布局)。


为了更加明显地看到这一点,想象一下这两个布局:

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

尽管在activity_main.xml中没有<fragment><include>标签,但是对于itemview.xml的布局编辑器预览仅会显示“first”和“second”。tools:showIn属性完全覆盖了正常的预览。


在编码时听到“不可能”的消息是令人沮丧的...但我想当涉及到 IDE 预览时,这就是一种交易。无论如何,感谢你的回答 :) - Samuel T. Chou

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接