代码中:
package com.qianfeng.createfragment2;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取fragment的管理对象
FragmentManager manager=getFragmentManager();
//将add remove replace 操作称为事务 创建事务操作对象
FragmentTransaction transaction=manager.beginTransaction();
//1参数标示指定的fragment添加到activity的区域的id 2标示需要添加的指定的fragment对象
transaction.add(R.id.content1, new Frgament1());
transaction.add(R.id.content2, new Frgament2());
//提交事务
transaction.commit();
}
}
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/content1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:id="@+id/content2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
</LinearLayout>
</LinearLayout>