1.把啟動的Activity改成從ActivityGroup繼承,在onCreate中setContentView(R.layout.activity_main);
2.activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="@string/hello_world"
android:textColor="#ffff0000"/>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/body"
android:background="#ffffffff"
android:layout_weight="1"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tabcontrol"
android:background="#ff00ff00"
>
<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:text="Button1"/>
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:text="Button2"/>
<Button android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:text="Button3"/>
</LinearLayout>
</LinearLayout>
3.對按鈕設定監聽函數,然後啟動新的activity,取到activity的view設定到body中
public void changeAcivity(String className)
{
String packageName =this.getPackageName();
Intent intent = new Intent();
intent.setClassName(packageName, className);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mView.removeAllViews();
mView.addView(getLocalActivityManager().startActivity(className, intent).getDecorView());
}
上代碼最省事:http://download.csdn.net/detail/wulongtiantang/5130528