好的,这可能有点棘手,但我想知道这是否可行。
假设我有一个主活动布局 - 它包含一个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
,该方法是否仍然适用?