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. publicclassMainViewextendsActivityGroup{
  2. @SuppressWarnings("unused")
  3. privateLinearLayoutbodyView,headview;
  4. privateLinearLayoutone,two,three,four;
  5. privateintflag=0;//通过标记跳转不同的页面,显示不同的菜单项
  6. /**Calledwhentheactivityisfirstcreated.*/
  7. @Override
  8. publicvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.view_main);
  11. initMainView();
  12. //显示标记页面
  13. showView(flag);
  14. one.setOnClickListener(newOnClickListener(){
  15. publicvoidonClick(Viewv){
  16. //TODOAuto-generatedmethodstub
  17. flag=0;
  18. showView(flag);
  19. }
  20. });
  21. two.setOnClickListener(newOnClickListener(){
  22. publicvoidonClick(Viewv){
  23. //TODOAuto-generatedmethodstub
  24. flag=1;
  25. showView(flag);
  26. }
  27. });
  28. three.setOnClickListener(newOnClickListener(){
  29. publicvoidonClick(Viewv){
  30. //TODOAuto-generatedmethodstub
  31. flag=2;
  32. showView(flag);
  33. }
  34. });
  35. four.setOnClickListener(newOnClickListener(){
  36. publicvoidonClick(Viewv){
  37. //TODOAuto-generatedmethodstub
  38. flag=3;
  39. showView(flag);
  40. }
  41. });
  42. }
  43. /*
  44. *初始化主界面
  45. */
  46. publicvoidinitMainView(){
  47. headview=(LinearLayout)findViewById(R.id.head);
  48. bodyView=(LinearLayout)findViewById(R.id.body);
  49. one=(LinearLayout)findViewById(R.id.one);
  50. two=(LinearLayout)findViewById(R.id.two);
  51. three=(LinearLayout)findViewById(R.id.three);
  52. four=(LinearLayout)findViewById(R.id.four);
  53. }
  54. //在主界面中显示其他界面
  55. publicvoidshowView(intflag){
  56. switch(flag){
  57. case0:
  58. bodyView.removeAllViews();
  59. Viewv=getLocalActivityManager().startActivity("one",
  60. newIntent(MainView.this,OneView.class)).getDecorView();
  61. one.setBackgroundResource(R.drawable.frame_button_background);
  62. two.setBackgroundResource(R.drawable.frame_button_nopressbg);
  63. three.setBackgroundResource(R.drawable.frame_button_nopressbg);
  64. four.setBackgroundResource(R.drawable.frame_button_nopressbg);
  65. bodyView.addView(v);
  66. break;
  67. case1:
  68. bodyView.removeAllViews();
  69. bodyView.addView(getLocalActivityManager().startActivity("two",
  70. newIntent(MainView.this,TwoView.class))
  71. .getDecorView());
  72. one.setBackgroundResource(R.drawable.frame_button_nopressbg);
  73. two.setBackgroundResource(R.drawable.frame_button_background);
  74. three.setBackgroundResource(R.drawable.frame_button_nopressbg);
  75. four.setBackgroundResource(R.drawable.frame_button_nopressbg);
  76. break;
  77. case2:
  78. bodyView.removeAllViews();
  79. bodyView.addView(getLocalActivityManager().startActivity(
  80. "three",newIntent(MainView.this,ThreeView.class))
  81. .getDecorView());
  82. one.setBackgroundResource(R.drawable.frame_button_nopressbg);
  83. two.setBackgroundResource(R.drawable.frame_button_nopressbg);
  84. three.setBackgroundResource(R.drawable.frame_button_background);
  85. four.setBackgroundResource(R.drawable.frame_button_nopressbg);
  86. break;
  87. case3:
  88. bodyView.removeAllViews();
  89. bodyView.addView(getLocalActivityManager().startActivity(
  90. "four",newIntent(MainView.this,FourView.class))
  91. .getDecorView());
  92. one.setBackgroundResource(R.drawable.frame_button_nopressbg);
  93. two.setBackgroundResource(R.drawable.frame_button_nopressbg);
  94. three.setBackgroundResource(R.drawable.frame_button_nopressbg);
  95. four.setBackgroundResource(R.drawable.frame_button_background);
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. }

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

        
  1. bodyView.removeAllViews();
  2. bodyView.addView(getLocalActivityManager().startActivity("two",
  3. newIntent(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功能了。

/**。
* @author 张兴业
* 邮箱:xy-zhang@163.com
* qq:363302850
*/

更多相关文章

  1. Visual Studio 跨平台开发实战(5) - Xamarin Android(安卓)多页
  2. Adnroid——自定义控件(入门篇之自定义验证码)
  3. Android定位功能(一)
  4. Android(安卓)高德地图 Polyline 设置点击事件
  5. Android事件的分发、拦截和执行
  6. Android(安卓)图片异步下载及缓存--Multithreading For Performa
  7. Android(安卓)gallery实现选中放大的效果
  8. Android_Servcie_后台服务总结笔记
  9. 对Android(安卓)ListView的理解

随机推荐

  1. LinearLayout中组件右对齐
  2. AOSP源码编译 --全部编译
  3. Android(安卓)手势识别--GestureDetector
  4. Android常用组件(View学习之一)
  5. android中-使用2D动画 — 图形处理(Canva
  6. 【Android多线程】线程二三事
  7. Android多国语言-国家代码清单
  8. Android(安卓)Activity生命周期(Android艺
  9. Android(安卓)属性动画(一)
  10. 屏蔽android的menu键,使menu键不能用,自定