zoukankan      html  css  js  c++  java
  • [转]Android UI开发第十八篇——ActivityGroup实现tab功能 by xyz_lmn

    本文转自:http://blog.devdiv.com/android-ui%e5%bc%80%e5%8f%91%e7%ac%ac%e5%8d%81%e5%85%ab%e7%af%87%e2%80%94%e2%80%94activitygroup%e5%ae%9e%e7%8e%b0tab%e5%8a%9f%e8%83%bd.html

    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 onetwothreefour;
    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=(LinearLayoutfindViewById(R.id.head);
    50.         bodyView=(LinearLayoutfindViewById(R.id.body);
    51.         one=(LinearLayoutfindViewById(R.id.one);
    52.         two=(LinearLayoutfindViewById(R.id.two);
    53.         three=(LinearLayoutfindViewById(R.id.three);
    54.         four=(LinearLayoutfindViewById(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.thisOneView.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.thisTwoView.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.thisThreeView.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.thisFourView.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.thisTwoView.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功能了。

  • 相关阅读:
    Java程序员必会的工具库,代码量减少90%
    Git常用操作
    Eclipse开发环境配置
    Spring Cloud Alibaba Nacos 在Windows10下启动
    MySQL连接异常Communications link failure
    Spring Cloud Alibaba Nacos2.0踩坑
    Spring Cloud Alibaba Nacos配置中心与服务发现
    RocketMQ学习笔记
    Linux开发环境配置
    Clumper尝鲜
  • 原文地址:https://www.cnblogs.com/freeliver54/p/2630783.html
Copyright © 2011-2022 走看看