利用ActivityGroup实现Tab分页标签

来源:互联网 发布:js代码大全 编辑:程序博客网 时间:2024/05/18 04:02
1 什么是ActivityGroup?

        让我们先看看官方的文档里是怎么说的。(PS:我们下载的SDK里面就有,打开docs文件夹,使用浏览器查看index.html。)

java.lang.Object

    ?android.content.Context

        ?android.content.ContextWrapper

            ?android.view.ContextThemeWrapper

                ?android.app.Activity

                    ?android.app.ActivityGroup

Class Overview

    A screen that contains and runs multiple embedded activities.

描述的很简单,就是在一个屏幕里包含及运行多个嵌入的Activity。从类的继承结构可以看出,ActivityGroup也是一个Activity,使用方法跟Activity类似。

2 ActivityGroup如何使用?

        为了便于大家理解,给出实现Tab分页标签的简单流程图。结构如下:

        首先新建一个Class,假如说命名为TestActivity,继承自ActivityGroup,重载protected void onCreate(Bundle savedInstanceState)方法,下列代码必不可少:

1: super.onCreate(savedInstanceState);    
2: setContentView(R.layout.main);

接下来需要看看main.xml该怎么写。

   1: <?xml version="1.0" encoding="utf-8"?> 
   2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   3:     android:orientation="vertical" 
   4:     android:layout_width="fill_parent" 
   5:     android:layout_height="fill_parent" 
   6:     android:gravity="bottom" 
   7:     android:id="@+id/parent"> 
   8:     <LinearLayout android:orientation="vertical" 
   9:                   android:layout_width="fill_parent" 
  10:                   android:layout_height="150dip" 
  11:                   android:id="@+id/head" 
  12:                   android:gravity="center" 
  13:                   android:background="@drawable/black" 
  14:                   android:layout_weight="0.95"> 
  15:         <TextView android:id="@+id/TextView1" 
  16:                   android:gravity="center_horizontal" 
  17:                   android:layout_width="wrap_content" 
  18:                   android:layout_height="40dip" 
  19:                   android:text="@string/app_name" 
  20:                   android:textSize="24px"> 
  21:         </TextView> 
  22:     </LinearLayout> 
  23:     <!--动态显示界面--> 
  24:     <LinearLayout android:orientation="vertical" 
  25:                   android:layout_width="fill_parent" 
  26:                   android:layout_height="fill_parent" 
  27:                   android:id="@+id/body" 
  28:                   android:background="@drawable/white" 
  29:                   android:layout_weight="0.95"> 
  30:     </LinearLayout> 
  31:     <!--底部功能菜单栏  --> 
  32:     <include android:id="@id/TabMenuView" layout="@layout/tab_menu_view" /> 
  33: </LinearLayout>

从xml文件我们可以看出,整个页面布局分成3部分,头部(Head),正文(Body),以及TAB菜单栏(TabMenu)。其中最关键的部分就是Body和TabMenu。Body负责显示嵌入到ActivityGroup中的Activity内容;TabMenu负责显示功能菜单栏。其中TabMenu的布局如下:

   1: <?xml version="1.0" encoding="UTF-8"?> 
   2: <LinearLayout android:orientation="horizontal" 
   3:     android:background="@drawable/black" 
   4:     android:layout_width="fill_parent" 
   5:     android:layout_height="wrap_content" 
   6:     android:weightSum="4.0" 
   7:   xmlns:android="http://schemas.android.com/apk/res/android"> 
   8:     <Button android:id="@id/ButtonHomeTab" 
   9:         android:padding="2.0dip" 
  10:         android:layout_width="106dip" 
  11:         android:layout_height="wrap_content" 
  12:         android:layout_marginRight="1.0dip" 
  13:         android:text="@string/strHomeTab" 
  14:         android:drawableTop="@drawable/home_grey" 
  15:         android:layout_weight="1.0" 
  16:         style="@style/TabItem" /> 
  17:     <Button android:id="@id/ButtonHistoryTab" 
  18:         android:padding="2.0dip" 
  19:         android:layout_width="106dip" 
  20:         android:layout_height="wrap_content" 
  21:         android:layout_marginRight="1.0dip" 
  22:         android:text="@string/strHistoryTab" 
  23:         android:drawableTop="@drawable/history_grey" 
  24:         android:layout_weight="1.0" 
  25:         style="@style/TabItem" /> 
  26:     <Button android:id="@id/ButtonSettingTab" 
  27:         android:padding="2.0dip" 
  28:         android:layout_width="106dip" 
  29:         android:layout_height="wrap_content" 
  30:         android:layout_marginRight="1.0dip" 
  31:         android:text="@string/strSettingTab" 
  32:         android:drawableTop="@drawable/setting_grey" 
  33:         android:layout_weight="1.0" 
  34:         style="@style/TabItem" /> 
  35: </LinearLayout>

看起来很简单,就是添加3个Button,用以切换ActivityGroup中嵌入的Activity。

        最后我们谈谈如何将Activity嵌入到ActivityGroup。

        从main.xml中可以看出,Body部分没有真正的内容,我们需要做的就是在新建的TestActivity中添加代码,用Activity的内容填充到ActivityGroup中。我们需要为下面的每一个Button添加监听事件,只有这样才能实现Body部分的内容切换。

   1: mBodyView=(LinearLayout) findViewById(R.id.body); 
   2: mButton[0]=(View) findViewById(R.id.Button1Tab); 
   3: mButton[1]=(View) findViewById(R.id.Button2Tab); 
   4: mButton[2]=(View) findViewById(R.id.Button3Tab); 
   5: public void setOnListener(){ 
   6:     for(int i=0;i<3;++i){ 
   7:         mButton[i].setOnClickListener(new OnClickListener() { 
   8:             public void onClick(View v) { 
   9:                 // TODO Auto-generated method stub 
  10:                 changeBodyContentView(v); 
  11:             } 
  12:         }); 
  13:     } 
  14: }

其中的changeBodyContentView(View)函数就是负责在TestActivity的Body部分显示Activity内容的函数。参数View就是被点击的Button,各位可以使用各种不同的方法实现Button对象与Activity的对应关系,这里就不详细讲了。

        现在我们知道每一个Button与Activity的对应关系,那么如何切换呢?见代码:

 

   1: Intent intent=new Intent(); 
   2: intent.setClassName(mPackageName, target); 
   3: intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
   4: mBodyView.removeAllViews(); 
   5: mBodyView.addView(getLocalActivityManager() 
   6:          .startActivity(target,intent).getDecorView());
哈哈,我们知道,如果想启动一个新的Activity,那么我们需要告诉Android系统我们的这个意图(Intent),然后将对象mBodyView中的所有视图去掉,启动对应的Activity,并添加到mBodyView中。但是我们需要注意的是,整个屏幕应该显示的内容是ActivityGroup的整体框架和嵌入其中的Activity的内容,所以我们使用addView的时候,别忘了调用getDecorView()函数。

(PS:SDK中getDecorView()的解释是Retrieve the top-level window decor view (containing the standard window frame/decorations and the client’s content inside of that), which can be added as a window to the window manager. )

        好了,我想讲的就是这些了,如果某位看官有问题,可以在下面的评论处提问,谢谢!

原创粉丝点击