ActivityGroup简介

ActivityGroup效果和TabHost效果类似。TabHost限制较多,自己定制不容易使用。

下面举例说明一下ActivityGroup的使用。

两个按钮,点击不同按钮切换不同的activity。

Java代码 复制代码 收藏代码
  1. private Button button1;  
  2.     private Button button2; 
  3.     private LinearLayout container; 
  4.     private OnClickListener l = new OnClickListener(){ 
  5.  
  6.         @Override 
  7.         public void onClick(View v) { 
  8.             // TODO Auto-generated method stub 
  9.             switch(v.getId()){ 
  10.             case R.id.button1: 
  11.                 switchActivity(0); 
  12.                 break
  13.             case R.id.button2: 
  14.                 switchActivity(1); 
  15.                 break
  16.             } 
  17.         } 
  18.          
  19.     }; 
  20.     @Override 
  21.     protected void onCreate(Bundle savedInstanceState) { 
  22.         // TODO Auto-generated method stub 
  23.         super.onCreate(savedInstanceState); 
  24.          
  25.         setContentView(R.layout.main); 
  26.          
  27.         button1 = (Button)findViewById(R.id.button1); 
  28.         button2 = (Button)findViewById(R.id.button2); 
  29.         container = (LinearLayout) findViewById(R.id.container); 
  30.          
  31.          
  32.         button1.setOnClickListener(l); 
  33.         button2.setOnClickListener(l); 
  34.          
  35.         switchActivity(0); 
  36.     } 
  37.      
  38.     private void switchActivity(int id){ 
  39.         container.removeAllViews(); 
  40.         Intent intent = null
  41.         switch(id){ 
  42.         case 0
  43.             intent = new Intent(this,TestActivity1.class); 
  44.             break
  45.         case 1
  46.             intent = new Intent(this,TestActivity2.class); 
  47.             break
  48.         } 
  49.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
  50.         Window  subActivity = getLocalActivityManager().startActivity("subActivity", intent); 
  51.         container.addView(subActivity.getDecorView(),LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
  52.     } 
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文件:

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"
  5.     <LinearLayout android:orientation="horizontal" 
  6.         android:layout_width="fill_parent" android:layout_height="wrap_content"
  7.         <Button android:id="@+id/button1" android:layout_width="wrap_content" 
  8.             android:layout_height="wrap_content" android:text="窗体1" /> 
  9.         <Button android:id="@+id/button2" android:layout_width="wrap_content" 
  10.             android:layout_height="wrap_content" android:text="窗体2" /> 
  11.     </LinearLayout> 
  12.     <LinearLayout android:id="@+id/container" android:orientation="horizontal" 
  13.         android:layout_width="fill_parent" android:layout_height="fill_parent" 
  14.         android:background="#0000ff"
  15.     </LinearLayout> 
  16. </LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout android:orientation="horizontal"
		android:layout_width="fill_parent" android:layout_height="wrap_content">
		<Button android:id="@+id/button1" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="窗体1" />
		<Button android:id="@+id/button2" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="窗体2" />
	</LinearLayout>
	<LinearLayout android:id="@+id/container" android:orientation="horizontal"
		android:layout_width="fill_parent" android:layout_height="fill_parent"
		android:background="#0000ff">
	</LinearLayout>
</LinearLayout>

 

从”窗口1“切换至”窗口2“时,事件执行的先后顺序如下:

INFO/TestActivity1(11718): onPause
INFO/TestActivity1(11718): onStop
INFO/TestActivity1(11718): onDestroy
INFO/TestActivity2(11718): onStart
INFO/TestActivity2(11718): onResume

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值