ActivityGroup实现iPhone风格的底部tab菜单

Java代码

  1. public class ActsGroup extends ActivityGroup {  
  2.  
  3. private LinearLayout bodyView;  
  4. private LinearLayout home, gamebox, team, more;  
  5. private int flag = 0; // 通过标记跳转不同的页面,显示不同的菜单项
  6. //  private String parameter = Constant.BUTTON_HOME;// 初始化加载
  7.  
  8. /** Called when the activity is first created. */
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {  
  11.         requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
  12. super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.acts_group);  
  14.         initMainView();  
  15. // 主界面开始接收参数
  16.         Bundle bundle = getIntent().getExtras();  
  17. if (null != bundle) {  
  18.             flag = bundle.getInt("flag");  
  19.         }  
  20. // 默认显示 
  21.         showView(flag);  
  22.  
  23.         home.setOnClickListener(new OnClickListener() {  
  24.  
  25. public void onClick(View v) {  
  26. // TODO Auto-generated method stub
  27.                 flag = 0;  
  28.                 showView(flag);  
  29.  
  30.             }  
  31.         });  
  32.         gamebox.setOnClickListener(new OnClickListener() {  
  33.  
  34. public void onClick(View v) {  
  35. // TODO Auto-generated method stub
  36.                 flag = 1;  
  37.                 showView(flag);  
  38.  
  39.             }  
  40.         });  
  41.         team.setOnClickListener(new OnClickListener() {  
  42.  
  43. public void onClick(View v) {  
  44. // TODO Auto-generated method stub
  45.                 flag = 2;  
  46.                 showView(flag);  
  47.  
  48.             }  
  49.         });  
  50.         more.setOnClickListener(new OnClickListener() {  
  51.  
  52. public void onClick(View v) {  
  53. // TODO Auto-generated method stub
  54.                 flag = 3;  
  55.                 showView(flag);  
  56.  
  57.  
  58.             }  
  59.         });  
  60.  
  61.     }  
  62.  
  63. /*
  64.      * 初始化主界面底部的功能菜单
  65.      */
  66. public void initMainView() {  
  67.         bodyView = (LinearLayout) findViewById(R.id.bodyL);  
  68.         home = (LinearLayout) findViewById(R.id.home);  
  69.         gamebox = (LinearLayout) findViewById(R.id.gamebox);  
  70.         team = (LinearLayout) findViewById(R.id.team);  
  71.         more = (LinearLayout) findViewById(R.id.more);  
  72.     }  
  73.  
  74. // 在主界面中显示其他界面
  75. public void showView(int flag) {  
  76. switch (flag) {  
  77. case 0:  
  78.             showHome();  
  79. break;  
  80. case 1:  
  81.             showGamebox();  
  82. break;  
  83. case 2:  
  84.             showTeam();  
  85. break;  
  86. case 3:  
  87.             showMore();  
  88. break;  
  89. default:  
  90. break;  
  91.         }  
  92.     }  
  93.  
  94. public void showHome() {  
  95.         bodyView.removeAllViews();  
  96.         bodyView.addView(getLocalActivityManager().startActivity("home",  
  97. new Intent(ActsGroup.this, MainActivity.class)).getDecorView());  
  98.         home.setBackgroundResource(R.drawable.tab_highlight);  
  99.         gamebox.setBackgroundResource(R.drawable.tab_background);  
  100.         more.setBackgroundResource(R.drawable.tab_background);  
  101.         team.setBackgroundResource(R.drawable.tab_background);  
  102.     }  
  103.  
  104. public void showGamebox() {  
  105.             bodyView.removeAllViews();  
  106.             bodyView.addView(getLocalActivityManager().startActivity(  
  107. "gamebox", new Intent(ActsGroup.this, Menu2Activity.class))  
  108.                     .getDecorView());  
  109.             gamebox.setBackgroundResource(R.drawable.tab_highlight);  
  110.             home.setBackgroundResource(R.drawable.tab_background);  
  111.             more.setBackgroundResource(R.drawable.tab_background);  
  112.             team.setBackgroundResource(R.drawable.tab_background);  
  113.     }  
  114.  
  115. public void showTeam() {  
  116.         bodyView.removeAllViews();  
  117.         bodyView.addView(getLocalActivityManager().startActivity("team",  
  118. new Intent(ActsGroup.this, Menu2Activity.class)).getDecorView());  
  119.         team.setBackgroundResource(R.drawable.tab_highlight);  
  120.         home.setBackgroundResource(R.drawable.tab_background);  
  121.         more.setBackgroundResource(R.drawable.tab_background);  
  122.         gamebox.setBackgroundResource(R.drawable.tab_background);  
  123.     }  
  124. public void showMore() {  
  125.         bodyView.removeAllViews();  
  126.         bodyView.addView(getLocalActivityManager().startActivity("more",  
  127. new Intent(ActsGroup.this, Menu2Activity.class)).getDecorView());  
  128.         more.setBackgroundResource(R.drawable.tab_highlight);  
  129.         home.setBackgroundResource(R.drawable.tab_background);  
  130.         team.setBackgroundResource(R.drawable.tab_background);  
  131.         gamebox.setBackgroundResource(R.drawable.tab_background);  
  132.     }  

public class ActsGroup extends ActivityGroup { private LinearLayout bodyView; private LinearLayout home, gamebox, team, more; private int flag = 0; // 通过标记跳转不同的页面,显示不同的菜单项 // private String parameter = Constant.BUTTON_HOME;// 初始化加载 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题 super.onCreate(savedInstanceState); setContentView(R.layout.acts_group); initMainView(); // 主界面开始接收参数 Bundle bundle = getIntent().getExtras(); if (null != bundle) { flag = bundle.getInt("flag"); } // 默认显示 showView(flag); home.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub flag = 0; showView(flag); } }); gamebox.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub flag = 1; showView(flag); } }); team.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub flag = 2; showView(flag); } }); more.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub flag = 3; showView(flag); } }); } /* * 初始化主界面底部的功能菜单 */ public void initMainView() { bodyView = (LinearLayout) findViewById(R.id.bodyL); home = (LinearLayout) findViewById(R.id.home); gamebox = (LinearLayout) findViewById(R.id.gamebox); team = (LinearLayout) findViewById(R.id.team); more = (LinearLayout) findViewById(R.id.more); } // 在主界面中显示其他界面 public void showView(int flag) { switch (flag) { case 0: showHome(); break; case 1: showGamebox(); break; case 2: showTeam(); break; case 3: showMore(); break; default: break; } } public void showHome() { bodyView.removeAllViews(); bodyView.addView(getLocalActivityManager().startActivity("home", new Intent(ActsGroup.this, MainActivity.class)).getDecorView()); home.setBackgroundResource(R.drawable.tab_highlight); gamebox.setBackgroundResource(R.drawable.tab_background); more.setBackgroundResource(R.drawable.tab_background); team.setBackgroundResource(R.drawable.tab_background); } public void showGamebox() { bodyView.removeAllViews(); bodyView.addView(getLocalActivityManager().startActivity( "gamebox", new Intent(ActsGroup.this, Menu2Activity.class)) .getDecorView()); gamebox.setBackgroundResource(R.drawable.tab_highlight); home.setBackgroundResource(R.drawable.tab_background); more.setBackgroundResource(R.drawable.tab_background); team.setBackgroundResource(R.drawable.tab_background); } public void showTeam() { bodyView.removeAllViews(); bodyView.addView(getLocalActivityManager().startActivity("team", new Intent(ActsGroup.this, Menu2Activity.class)).getDecorView()); team.setBackgroundResource(R.drawable.tab_highlight); home.setBackgroundResource(R.drawable.tab_background); more.setBackgroundResource(R.drawable.tab_background); gamebox.setBackgroundResource(R.drawable.tab_background); } public void showMore() { bodyView.removeAllViews(); bodyView.addView(getLocalActivityManager().startActivity("more", new Intent(ActsGroup.this, Menu2Activity.class)).getDecorView()); more.setBackgroundResource(R.drawable.tab_highlight); home.setBackgroundResource(R.drawable.tab_background); team.setBackgroundResource(R.drawable.tab_background); gamebox.setBackgroundResource(R.drawable.tab_background); } }

Xml代码

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_;fill_parent" android:orientation="vertical"
  3. android:layout_height="fill_parent">
  4. <!--动态显示界面-->
  5. <LinearLayout android:orientation="vertical"
  6. android:layout_;fill_parent"
  7. android:layout_height="fill_parent"
  8. android:id="@+id/bodyL"
  9. android:layout_weight="0.95">
  10. </LinearLayout>
  11. <!--底部功能菜单栏  -->
  12. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  13. android:id="@+id/bottomlist"
  14. android:orientation="horizontal"
  15. android:layout_;fill_parent"
  16. android:layout_height="wrap_content"
  17. android:layout_weight="0.05">
  18. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  19. android:orientation="vertical"
  20. android:layout_;wrap_content"
  21. android:layout_height="fill_parent"
  22. android:id="@+id/home"
  23. android:layout_weight="5"
  24. android:gravity="center_horizontal">
  25. <ImageView android:background="@drawable/home"
  26. android:layout_gravity="top|center"
  27. android:layout_height="35dp"
  28. android:layout_;32dp"
  29. android:layout_marginTop="4dp"/>
  30. <TextView android:layout_;wrap_content"
  31. android:layout_height="20dp"
  32. android:text="@string/home"
  33. />
  34. </LinearLayout>
  35. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  36. android:orientation="vertical"
  37. android:layout_;wrap_content"
  38. android:layout_height="fill_parent"
  39. android:id="@+id/gamebox"
  40. android:layout_weight="5"
  41. android:gravity="center_horizontal">
  42. <ImageView android:background="@drawable/gamebox"
  43. android:layout_gravity="top|center"
  44. android:layout_height="35dp"
  45. android:layout_;32dp"
  46. android:layout_marginTop="4dp"/>
  47. <TextView android:layout_;wrap_content"
  48. android:layout_height="20dp"
  49. android:text="@string/gamebox"
  50. />
  51. </LinearLayout>
  52. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  53. android:orientation="vertical"
  54. android:layout_;wrap_content"
  55. android:id="@+id/team"
  56. android:layout_height="fill_parent"
  57. android:layout_weight="5"
  58. android:gravity="center_horizontal">
  59. <ImageView android:background="@drawable/team"
  60. android:layout_gravity="top|center"
  61. android:layout_height="35dp"
  62. android:layout_;32dp"
  63. android:layout_marginTop="4dp"
  64. />
  65. <TextView android:layout_;wrap_content"
  66. android:layout_height="20dp"
  67. android:text="@string/team"
  68. />
  69. </LinearLayout>
  70. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  71. android:orientation="vertical"
  72. android:layout_;wrap_content"
  73. android:id="@+id/more"
  74. android:layout_height="fill_parent"
  75. android:layout_weight="5"
  76. android:gravity="center_horizontal">
  77. <ImageView android:background="@drawable/more"
  78. android:layout_gravity="top|center"
  79. android:layout_height="35dp"
  80. android:layout_;32dp"
  81. android:layout_marginTop="4dp"
  82. />
  83. <TextView android:layout_;wrap_content"
  84. android:layout_height="20dp"
  85. android:text="@string/more"
  86. />
  87. </LinearLayout>
  88. </LinearLayout>
  89. </LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_;fill_parent" android:orientation="vertical" android:layout_height="fill_parent"> <!--动态显示界面--> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/bodyL" android:layout_weight="0.95"> </LinearLayout> <!--底部功能菜单栏 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/bottomlist" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.05"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/home" android:layout_weight="5" android:gravity="center_horizontal"> <ImageView android:background="@drawable/home" android:layout_gravity="top|center" android:layout_height="35dp" android:layout_width="32dp" android:layout_marginTop="4dp"/> <TextView android:layout_width="wrap_content" android:layout_height="20dp" android:text="@string/home" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/gamebox" android:layout_weight="5" android:gravity="center_horizontal"> <ImageView android:background="@drawable/gamebox" android:layout_gravity="top|center" android:layout_height="35dp" android:layout_width="32dp" android:layout_marginTop="4dp"/> <TextView android:layout_width="wrap_content" android:layout_height="20dp" android:text="@string/gamebox" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:id="@+id/team" android:layout_height="fill_parent" android:layout_weight="5" android:gravity="center_horizontal"> <ImageView android:background="@drawable/team" android:layout_gravity="top|center" android:layout_height="35dp" android:layout_width="32dp" android:layout_marginTop="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="20dp" android:text="@string/team" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:id="@+id/more" android:layout_height="fill_parent" android:layout_weight="5" android:gravity="center_horizontal"> <ImageView android:background="@drawable/more" android:layout_gravity="top|center" android:layout_height="35dp" android:layout_width="32dp" android:layout_marginTop="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="20dp" android:text="@string/more" /> </LinearLayout> </LinearLayout> </LinearLayout>

你可能感兴趣的:(实现,ActivityGroup,休闲,tab菜单,iPhone风格)