ActivityGroup实现Tab效果

相对于TabActivity来说,ActivityGroup实现Tab更加灵活。

xml布局文件:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <LinearLayout    
  8.         android:id="@+id/containerBody"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:layout_weight="1"  
  12.         android:orientation="vertical" ></LinearLayout>  
  13.   
  14.     <LinearLayout  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="match_parent"   
  17.         android:layout_weight="5"  
  18.         android:orientation="horizontal">  
  19.   
  20.         <ImageView  
  21.             android:id="@+id/imageView1"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="match_parent"  
  24.             android:src="@drawable/toolbar_cost" />  
  25.   
  26.         <ImageView  
  27.             android:id="@+id/imageView2"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="match_parent"  
  30.             android:src="@drawable/toolbar_count" />  
  31.           
  32.     </LinearLayout>  
  33.   
  34. </LinearLayout>  

Activity类:

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. package com.zzj.ui;  
  2.   
  3. import android.app.ActivityGroup;  
  4. import android.app.LocalActivityManager;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.ImageView;  
  10. import android.widget.LinearLayout;  
  11.   
  12. @SuppressWarnings("deprecation")  
  13. public class MyActivityGroup extends ActivityGroup {  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activitygroup);  
  18.         init();  
  19.     }  
  20.   
  21.     private void init() {  
  22.         final LinearLayout containerBody = (LinearLayout) this  
  23.                 .findViewById(R.id.containerBody);  
  24.         final LocalActivityManager activityManager = getLocalActivityManager();//获取Activity管理器  
  25.         containerBody.addView(activityManager.startActivity("tag1",  
  26.                 new Intent(MyActivityGroup.this, Tab1.class)).getDecorView());  
  27.   
  28.         ImageView imageView = (ImageView) this.findViewById(R.id.imageView1);  
  29.         imageView.setOnClickListener(new OnClickListener() {  
  30.   
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 containerBody.removeAllViews();  
  34.                 containerBody.addView(activityManager.startActivity(  
  35.                         "tag1",  
  36.                         new Intent(MyActivityGroup.this, Tab1.class)  
  37.                                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  38.                         .getDecorView());  
  39.             }  
  40.         });  
  41.   
  42.         ImageView imageView2 = (ImageView) this.findViewById(R.id.imageView2);  
  43.         imageView2.setOnClickListener(new OnClickListener() {  
  44.   
  45.             @Override  
  46.             public void onClick(View v) {  
  47.                 containerBody.removeAllViews();  
  48.                 containerBody.addView(activityManager.startActivity(  
  49.                         "tag2",  
  50.                         new Intent(MyActivityGroup.this, Tab2.class)  
  51.                                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  52.                         .getDecorView());  
  53.             }  
  54.         });  
  55.     }  
  56. }  

Tab1和Tab2的布局文件和类文件就省略了,就是普通的Activity。

 

效果:

posted @ 2016-11-28 16:07  天涯海角路  阅读(62)  评论(0)    收藏  举报