替换包含布局
我在Android Studio中生成了新的Android项目,其中我选择了带有片段的空白活动的新模板,我发现它的结构与旧的不同。它使用标签和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>
别担心。
您只需在content_main.xml文件内完成所有设计。
它将被包含在activity_main.xml文件中,该文件是您的活动的主要布局。
是的,我知道,但如果我想要programaticaly将此content.xml更改为content2.xml? – HyperX
请你详细说明一下吗? –
用容器替换include
例如, FrameLayout
。在运行时充气您想要添加的布局,并在容器上调用addView
。若要替换已有的内容,请拨removeView
,然后addView
与新内容
如果我想为每个片段设置不同的工具栏,该怎么办?内容会改变,但工具栏将保持不变? – user3289108
@ user3289108,它确实会保持不变 – Blackbelt
但是,如果我想有不同的工具栏,那我该怎么办?我刚才提出了一个问题。你可以看看这个http://stackoverflow.com/questions/34760818/different-toolbar-for-each-fragment-in-new-coordinator-layout – user3289108
只需放置您的布局代码并删除包含如果您不想包含 – Pavan
您的@ layout/content_main有什么内容? –
简单的布局,如登录和注册形式,我知道我可以把布局,但我喜欢它的方式。所以它分离在不同的文件 – HyperX