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.          
  50.         Window  subActivity = getLocalActivityManager().startActivity("subActivity", intent);  
  51.         container.addView(subActivity.getDecorView(),LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  52.     }  

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>  

 

轉載:http://gjhappyyy.iteye.com/blog/1258279

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章