方法一:
通过在Activity类中构建两个字页面的对象,嵌入到主页面中(android:id="@+id/container"主、子页面相同)
主UI:activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"--必填
android:id="@+id/container" --必填
android:layout_width="match_parent" --必填
android:layout_height="match_parent"> --必填
</FrameLayout>
子UI:fragment_main.xml 和 fragment_main2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</RelativeLayout>
Activity:MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//渲染主页面activity_main
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment2()).commit();
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);//加载子页面fragment_main
return rootView;
}
}
public static class PlaceholderFragment2 extends Fragment {
public PlaceholderFragment2() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container,false);//加载子页面fragment_main2
return rootView;
}
}