在我的一个片段中,我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/email_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="@drawable/banner_dashboard" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<include layout="@layout/settings_nested" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
现在如下:
<include layout="@layout/settings_nested" />
由一些按钮组成,点击一个这样的按钮,我想用新的内容替换框架布局——具有不同布局的新片段。以下是我的操作方法:
Fragment fragment = null;
fragment = new FragmentEmails();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.email_container, fragment);
fragmentTransaction.commit();
现在,当我单击按钮时,我可以看到选项菜单发生了变化(因为我已经为这个新片段定义了一个不同的菜单文件)。但是内容保持不变。有什么建议吗?我认为我把framlayout定义为我的视图的父级的方式做错了什么,但确切地说,这让我很沮丧。
根据我的想法和观点,你应该按照以下
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/email_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
这是所有片段的父布局,email_container将用于其他片段的事务。
然后创建另一个布局,由我删除的内部元素组成。
在启动时,将这个email_container inneer元素更改为另一个片段,如
Fragment fragmentStartPage = null;
fragmentStartPage = new FragmentEmails();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.email_container, fragmentStartPage);
fragmentTransaction.commit();
然后通过将fragmentStartPage替换为另一个片段来将其更改为另一片段。