替换包括布局

时间:2015-10-15 11:31:16

标签: android android-fragments android-coordinatorlayout

我在Android Studio中生成了新的Android项目,我选择了Fragment的空白活动的新模板,我发现它的结构与旧的有点不同。它使用tag和CoordinatorLayout。那么我应该如何在这个包含标签内切换布局?

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

2 个答案:

答案 0 :(得分:3)

include替换为容器,例如FrameLayout。在运行时膨胀您要添加的布局,并在容器上调用addView。要替换您已有的内容,请使用新内容

调用removeView然后addView

答案 1 :(得分:0)

不要担心。

您只需在content_main.xml文件中完成所有设计。

它将包含在activity_main.xml文件中,该文件是您活动的主要布局。