ActivityGroup效果和TabHost效果类似。TabHost限制较多,自己定制不容易使用。
下面举例说明一下ActivityGroup的使用。
两个按钮,点击不同按钮切换不同的activity。
- private
Button button1; -
private Button button2; -
private LinearLayout container; -
private OnClickListener l = new OnClickListener(){ -
-
@Override -
public void onClick(View v) { -
// TODO Auto-generated method stub -
switch(v.getId()){ -
case R.id.button1: -
switchActivity(0); -
break; -
case R.id.button2: -
switchActivity(1); -
break; -
} -
} -
-
}; -
@Override -
protected void onCreate(Bundle savedInstanceState) { -
// TODO Auto-generated method stub -
super.onCreate(savedInstanceState); -
-
setContentView(R.layout.main); -
-
button1 = (Button)findViewById(R.id.button1); -
button2 = (Button)findViewById(R.id.button2); -
container = (LinearLayout) findViewById(R.id.container); -
-
-
button1.setOnClickListener(l); -
button2.setOnClickListener(l); -
-
switchActivity(0); -
} -
-
private void switchActivity(int id){ -
container.removeAllViews(); -
Intent intent = null; -
switch(id){ -
case 0: -
intent = new Intent(this,TestActivity1.class); -
break; -
case 1: -
intent = new Intent(this,TestActivity2.class); -
break; -
} -
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); -
Window subActivity = getLocalActivityManager().startActivity("subActivity", intent); -
container.addView(subActivity.getDecorView(),LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); -
}
xml文件:
- <<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.200000762939453px; background-color: rgb(255, 255, 255);">xml文件?xml
version="1.0" encoding="utf-8"?xml文件> - "http://schemas.android.com/apk/res/android"
-
android:orientation="vertical" android:layout_width="fill_parent" -
android:layout_height="fill_parent"> -
"horizontal" -
android:layout_width="fill_parent" android:layout_height="wrap_content"> -
"@+id/button1" android:layout_width="wrap_content" -
android:layout_height="wrap_content" android:text="窗体1" /> -
"@+id/button2" android:layout_width="wrap_content" -
android:layout_height="wrap_content" android:text="窗体2" /> -
-
"@+id/container" android:orientation="horizontal" -
android:layout_width="fill_parent" android:layout_height="fill_parent" -
android:background="#0000ff"> -
- <<b style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.200000762939453px;">xml文件/LinearLayoutxml文件>
INFO/TestActivity1(11718): onPause
INFO/TestActivity1(11718): onStop
INFO/TestActivity1(11718): onDestroy
INFO/TestActivity2(11718): onStart
INFO/TestActivity2(11718): onResume