zoukankan      html  css  js  c++  java
  • ActivityGroup实现tab功能

     android.app包中含有一个ActivityGroup类,该类是Activity的容器,可以包含多个嵌套进来的 Activitys,这篇文章就是借助ActivityGroup可以嵌套Activity的功能来实现Tab功能。tab这种UI在很多的移动应用中可 以看到,包括android、iphone、window phone7等移动终端上都有这样的应用,Tab这种UI方式具有小视图大容量的特点。
           首先,从SDK中doc文档中都可以获知,ActivityGroup类的父类是Activity(见下图),也就是说二者具有相同的接口和生命周期,同Activity一样,也有onCreate()、onPause()等函数可供我们重载。


    ActivityGroup中有两个public方法(下图):ActivityGroup中可以调用getLocalActivityManage()方法获取LocalActityManage来管理Activity。


    ActivityGroup实现的tab功能的效果图如下。


    先看一下java代码:  

    1. public class MainView extends ActivityGroup {  
    2.     @SuppressWarnings("unused")  
    3.     private LinearLayout bodyView,headview;  
    4.     private LinearLayout one, two, three, four;  
    5.     private int flag = 0; // 通过标记跳转不同的页面,显示不同的菜单项  
    6.     /** Called when the activity is first created. */  
    7.     @Override  
    8.     public void onCreate(Bundle savedInstanceState) {  
    9.         super.onCreate(savedInstanceState);  
    10.         setContentView(R.layout.view_main);  
    11.         initMainView();  
    12.         // 显示标记页面  
    13.         showView(flag);  
    14.         one.setOnClickListener(new OnClickListener() {        
    15.             public void onClick(View v) {  
    16.                 // TODO Auto-generated method stub  
    17.                 flag = 0;  
    18.                 showView(flag);  
    19.                 }  
    20.         });  
    21.         two.setOnClickListener(new OnClickListener() {                    
    22.             public void onClick(View v) {  
    23.                 // TODO Auto-generated method stub  
    24.                 flag = 1;  
    25.                 showView(flag);  
    26.             }  
    27.         });  
    28.         three.setOnClickListener(new OnClickListener() {                  
    29.             public void onClick(View v) {  
    30.                 // TODO Auto-generated method stub  
    31.                 flag = 2;  
    32.                 showView(flag);  
    33.             }  
    34.         });  
    35.         four.setOnClickListener(new OnClickListener() {           
    36.                 public void onClick(View v) {  
    37.                 // TODO Auto-generated method stub  
    38.                     flag = 3;  
    39.                     showView(flag);  
    40.             }  
    41.         });  
    42.   
    43.     }  
    44.      
    45.     /*
    46.      * 初始化主界面
    47.      */  
    48.     public void initMainView() {  
    49.         headview=(LinearLayout) findViewById(R.id.head);  
    50.         bodyView=(LinearLayout) findViewById(R.id.body);  
    51.         one=(LinearLayout) findViewById(R.id.one);  
    52.         two=(LinearLayout) findViewById(R.id.two);  
    53.         three=(LinearLayout) findViewById(R.id.three);  
    54.         four=(LinearLayout) findViewById(R.id.four);  
    55.     }  
    56.       
    57.    // 在主界面中显示其他界面  
    58.     public void showView(int flag) {  
    59.         switch (flag) {  
    60.         case 0:  
    61.             bodyView.removeAllViews();  
    62.             View v = getLocalActivityManager().startActivity("one",  
    63.                     new Intent(MainView.this, OneView.class)).getDecorView();  
    64.   
    65.             one.setBackgroundResource(R.drawable.frame_button_background);  
    66.             two.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    67.             three.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    68.             four.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    69.          
    70.             bodyView.addView(v);  
    71.             break;  
    72.         case 1:  
    73.             bodyView.removeAllViews();  
    74.             bodyView.addView(getLocalActivityManager().startActivity("two",  
    75.                     new Intent(MainView.this, TwoView.class))  
    76.                     .getDecorView());  
    77.             one.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    78.             two.setBackgroundResource(R.drawable.frame_button_background);  
    79.             three.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    80.             four.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    81.             break;  
    82.         case 2:           
    83.             bodyView.removeAllViews();  
    84.             bodyView.addView(getLocalActivityManager().startActivity(  
    85.                     "three", new Intent(MainView.this, ThreeView.class))  
    86.                     .getDecorView());  
    87.             one.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    88.             two.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    89.             three.setBackgroundResource(R.drawable.frame_button_background);  
    90.             four.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    91.             break;  
    92.         case 3:           
    93.             bodyView.removeAllViews();  
    94.             bodyView.addView(getLocalActivityManager().startActivity(  
    95.                     "four", new Intent(MainView.this, FourView.class))  
    96.                     .getDecorView());  
    97.             one.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    98.             two.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    99.             three.setBackgroundResource(R.drawable.frame_button_nopressbg);  
    100.             four.setBackgroundResource(R.drawable.frame_button_background);  
    101.             break;  
    102.         default:  
    103.             break;  
    104.         }  
    105.     }  
    106. }  

    程序中重要的是如下的方法:

    1. bodyView.removeAllViews();  
    2.     bodyView.addView(getLocalActivityManager().startActivity("two",  
    3. new Intent(MainView.this, TwoView.class))  
    4. .getDecorView());  

    使用view的removeAllViews()方法清除不需要的view,使用addView(View v)方法添加需要的view。
    getLocalActivityManager().startActivity("two",new Intent(MainView.this, TwoView.class))得到一个window对象,window对象调用
    getDecorView()获取view。关于window的方法可以参考android.app.Window。
          通过tab的效果图可以看到这个效果使用了上、中、下三种布局,layout就可以这样做了。实现layout就可以实现tab功能了。

  • 相关阅读:
    pod
    jquery日历插件
    web前端常用api
    VUE.JS——脚手架安装
    github入门到上传本地项目
    appach修改默认端口之后数据库的访问
    在脚本中刷新impala元信息
    在脚本中刷新impala元信息
    不同hadoop集群之间迁移hive数据
    不同hadoop集群之间迁移hive数据
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3256424.html
Copyright © 2011-2022 走看看