TabActivity,TabHost,

本文详细解析了TabActivity的基本使用方法,包括如何通过TabHost实现标签页功能,展示了最简单的TabActivity示例,并提供了TabWidget的内部布局结构分析。此外,还介绍了如何自定义TabHost布局文件,以及TabHost中的子Activity是如何通过Intent参数启动的。最后讨论了ActivityGroup与Tab切换的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.TabActivity 继承自Activity,其内部定义好了TabHost,可以通过getTabHost()获取TabHost。

    TabHost 包含了两种子元素:一些可以自由选择的Tab,及 与这些tab对应的内容tabContent,在layout的<TabHost>下它们分别对应 TabWidget (用于展示标签页, id固定为tabs )和 FrameLayout(用于展示隶属于各个标签的具体布局, id固定为tabcontent)。

Tabhost默认的布局文件:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"   
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent"     
     android:layout_height="fill_parent" > 
  <LinearLayout     
      android:orientation="vertical" 
      android:gravity="bottom" 
      android:layout_width="fill_parent"     
      android:layout_height="fill_parent" > 


      <TabWidget     
       android:id="@android:id/tabs"   
       android:layout_width="fill_parent"     
       android:layout_height="wrap_content" />  


       <FrameLayout     
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent"     
       android:layout_height="200dip" >
      </FrameLayout> 


  </LinearLayout>     
</TabHost>  

 

2.最简单的TabActivity示例,不用设置setContentView(R.layout.main);  ,不依靠布局文件:

public class ILoveConactActivity  extends TabActivity implements TabHost.TabContentFactory,OnTabChangeListener
{
    public void onCreate(Bundle icicle)
    {  
        super.onCreate(icicle);   
       
       //取得TabHost对象
       TabHost tabHost=this.getTabHost();
       tabHost.setOnTabChangedListener(this);
     
       for(int i=0;i<4;i++)
       {

          String tabItemTitle="tab"+i;
          String tabItemTag=""+i;


          TabSpec tabSpec=tabHost.newTabSpec(tabItemTag);
          tabSpec.setIndicator(tabItemTitle);//或者setIndicator(CharSequence label, Drawable icon) ,setIndicator(View view) 

 

          /*

             或者tabSpec.setContent(R.id.viewId);//R.id.viewId是在布局文件中的view的id。,setIndicator(View view),

             setContent(TabContentFactory contentFactory)时会动态确定内容。

             setContent(Intent intent) Specify an intent to use to launch an activity as the tab content.

          */

          tabSpec.setContent(this);


 
          tabHost.addTab(tabSpec);
      }
    
       tabHost.setCurrentTab(1);//或者setCurrentTabByTag(String tag)

  

      //加上下面这4行代码,使TabWidget到底部:

      TabWidget tabWidget = this.getTabWidget();  //或者tabHost.getTabWidget();

      LinearLayout  layout= (LinearLayout) tabHost.getChildAt(0);

      layout.removeViewAt(0);

      layout.addView(tabWidget);//把tabWidget 从第一个位置移除,再添加到下面来。
 }

   @Override
   public void onTabChanged(String tabId)
   {
      // TODO Auto-generated method stub
   }

   @Override
   public View createTabContent(String tag)
   {
      TextView view=new TextView(this);
      view.setText("tab content "+tag);
      return view;//不能返回null,否则会报错。
   }  

}

 

intent必须是一个关联Activity的intent。

 http://www.cnblogs.com/over140/archive/2010/12/13/1904085.html 

 

3.对TabWidget的理解: 

    (1)TabWidget 为 horizontal 的 LinearLayout 
    (2)且 其包含的标签又是一个RelativeLayout 
    (3) 每个标签RelativeLayout 里面包含2个View:TextView和ImageView

     推算出其布局为:

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<RelativeLayout
 android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
 <ImageView  />
    <TextView  />
</RelativeLayout>
<RelativeLayout
 android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
 <ImageView  />
    <TextView  />
</RelativeLayout>
<RelativeLayout
 android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
 <ImageView  />
    <TextView  />
</RelativeLayout>
<RelativeLayout
 android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
 <ImageView  />
    <TextView  />
</RelativeLayout>
</LinearLayout>

 

4.基于默认的Tabhost布局文件,自定义布局文件:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">


    <!-- TabHost必须包含一个 TabWidget和一个FrameLayout-->
    <TabHost android:id="@+id/tabhost"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      >


      <LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">


   <!-- TabWidget的id属性必须为 @android:id/tabs-->  
       <TabWidget android:id="@android:id/tabs"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
         </TabWidget>


       <!-- FrameLayout的id属性必须为 @android:id/tabcontent-->
       <FrameLayout android:id="@android:id/tabcontent"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
          <TextView android:id="@+id/view1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
        <TextView android:id="@+id/view2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
        <TextView android:id="@+id/view3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
       </FrameLayout>
     
      </LinearLayout>
    </TabHost>
</LinearLayout>
5.TabHost中的子Activity虽然是用Intent参数来启动,但实际上并没有去启动一个全新的Activity。维护了几个子View

6.ActivityGroup的问题:

   它是用来做Tab切换用的,不是用做栈导航的;

   在一个ActivityGroup中连续开启同一个Activity,第2个及之后和第1个在创建菜单的函数调用顺序上不同;

   在一个ActivityGroup中连续开启同一个Activity非同一个Activity,后面的创建菜单的函数不再调用了...

   另外也没有StartActivityForResult函数。
   ActivityGroup中可以指定界面中的各个部分内容来自不同的Activity,这时这些Actvitiy只作为内容管理单元。

  

  

 

 

发送邮件失败,检查个人信息中的姓名,邮件地址是否填写了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值