2012CSDN年度博客之星评选期待您的参与

http://vote.blog.csdn.net/item/blogstar/xyz_lmn

          

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. public void initMainView() {
  48. headview=(LinearLayout) findViewById(R.id.head);
  49. bodyView=(LinearLayout) findViewById(R.id.body);
  50. one=(LinearLayout) findViewById(R.id.one);
  51. two=(LinearLayout) findViewById(R.id.two);
  52. three=(LinearLayout) findViewById(R.id.three);
  53. four=(LinearLayout) findViewById(R.id.four);
  54. }
  55. // 在主界面中显示其他界面
  56. public void showView(int flag) {
  57. switch (flag) {
  58. case 0:
  59. bodyView.removeAllViews();
  60. View v = getLocalActivityManager().startActivity("one",
  61. new Intent(MainView.this, OneView.class)).getDecorView();
  62.  
  63. one.setBackgroundResource(R.drawable.frame_button_background);
  64. two.setBackgroundResource(R.drawable.frame_button_nopressbg);
  65. three.setBackgroundResource(R.drawable.frame_button_nopressbg);
  66. four.setBackgroundResource(R.drawable.frame_button_nopressbg);
  67. bodyView.addView(v);
  68. break;
  69. case 1:
  70. bodyView.removeAllViews();
  71. bodyView.addView(getLocalActivityManager().startActivity("two",
  72. new Intent(MainView.this, TwoView.class))
  73. .getDecorView());
  74. one.setBackgroundResource(R.drawable.frame_button_nopressbg);
  75. two.setBackgroundResource(R.drawable.frame_button_background);
  76. three.setBackgroundResource(R.drawable.frame_button_nopressbg);
  77. four.setBackgroundResource(R.drawable.frame_button_nopressbg);
  78. break;
  79. case 2:
  80. bodyView.removeAllViews();
  81. bodyView.addView(getLocalActivityManager().startActivity(
  82. "three", new Intent(MainView.this, ThreeView.class))
  83. .getDecorView());
  84. one.setBackgroundResource(R.drawable.frame_button_nopressbg);
  85. two.setBackgroundResource(R.drawable.frame_button_nopressbg);
  86. three.setBackgroundResource(R.drawable.frame_button_background);
  87. four.setBackgroundResource(R.drawable.frame_button_nopressbg);
  88. break;
  89. case 3:
  90. bodyView.removeAllViews();
  91. bodyView.addView(getLocalActivityManager().startActivity(
  92. "four", new Intent(MainView.this, FourView.class))
  93. .getDecorView());
  94. one.setBackgroundResource(R.drawable.frame_button_nopressbg);
  95. two.setBackgroundResource(R.drawable.frame_button_nopressbg);
  96. three.setBackgroundResource(R.drawable.frame_button_nopressbg);
  97. four.setBackgroundResource(R.drawable.frame_button_background);
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. }

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

  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功能了。

 

/**
* @author 张兴业
* 邮箱:xy-zhang#163.com
* android开发进阶群:278401545
*
*/

      

 

 

版权声明:本文为xyzlmn原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xyzlmn/archive/2011/11/05/3168160.html