我创建了一个带有少量导航项目(使用片段)的工作导航抽屉。
当应用程序启动时,它会显示带有空白页面的主要活动,因为主要活动上设置的布局仅包含刷过屏幕时显示的导航项目。
当点击导航项时,它们工作正常。但是当我在mainactivity布局文件中更改某些内容(如添加textview)然后打开任何导航项时,这些项打开了受尊重的片段,但布局仍然是主要活动,包括textview而不是片段布局。
那么,如何在不影响导航项的情况下在Mainactivity布局文件中添加内容?
这是我的主要活动布局xml。
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="@+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="this textview appears in all fragment"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp"
android:layout_marginStart="66dp"
android:layout_marginTop="30dp"
/>
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.example.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
当使用此textview时,将为每个片段显示此布局,如果未使用,则使用片段的布局。