TabActivity & TabHost 的使用

       这段时间在研究android平台上的开源项目——StandupTimer,这是由jwood所设计的一个较为简单android应用,用于控制会议时间,类似秒表倒计时。
      tabActivity继承自Activity,其内部定义好了TabHost,可以通过getTabHost()获取。TabHost 包含了两种子元素:一些可以自由选择的Tab 和tab对应的内容tabContent,在Layout的<TabHost>下它们分别对应 TabWidget 和 FrameLayout。
  使用 TabActivity 可以让同一个界面容纳更多的内容。我们将按照Standup Timer里的TeamDetailsActivity来讲述TabActivity的使用。在该例中,包含了两个Tab一个用于展示team的统计信息,一个用于展示team所参加的会议的列表(这是一个ListView)。

创建Layout 

  这里需要注意的是不管你是使用TabActivity 还是自定义TabHost,都要求以TabHost作为XML布局文件的根。
<?xml version="1.0" encoding="utf-8"?>
<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: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="fill_parent">


<!--省略部分代码-->

<TextView android:id="@+id/no_team_meetings"
android:textSize="18sp" android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<TextView android:id="@+id/no_team_meeting_stats"
android:textSize="18sp" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
      通常我们采用线性布局 ,所以<TabHost> 的子元素是 <LinearLayout>。<TabWidget>对应Tab。<FrameLayout>则用于包含Tab需要展示的内容。需要注意的是<TabWidget> 和<FrameLayout>的Id 必须使用系统id,分别为android:id/tabs 和 android:id/tabcontent 。因为系统会使用这两个id来初始化TabHost的两个实例变量(mTabWidget 和 mTabContent)。

编写Java代码

  我们可以采用两种方法编写标签页:一种是继承TabActivity ,然后使用getTabHost()获取TabHost对象;第二种方法是使用自定的TabHost在布局文件上<TabHost>的自定义其ID,然后通过findViewById(),方法获得TabHost对象。
  本文中采用继承TabActivity的方法。
private void createTabs() {
TabHost tabhost=getTabHost();
tabhost.addTab(tabhost.newTabSpec("stats_tab").
setIndicator(this.getString(R.string.stats)).
setContent(createMeetingDetails(team)));

tabhost.addTab(tabhost.newTabSpec("meetings_tab").
setIndicator(this.getString(R.string.meetings)).
setContent(createMeetingList()));
getTabHost().setCurrentTab(0);
}
       Java代码中我们首先需要做的是获取TabHost对象,可以通过TabActivtiy里的getTabHsot()方法。如果是自定义TabHost,在添加Tabs前 应该调用  setUp() 方法。
          
          
            
            
mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, " Hello, world! " , " Tab 1 " );

SDK上的原文:
  Call setup() before adding tabs if loading TabHost using findViewById(). However You do not need to call setup() 
after getTabHost() in TabActivity

   接着向TabHost添加tabs.即调用tabHost.addTab(TabSpec) 方法。 TabSpec主要包含了setIndicator () 和
setContent() 方法,通过这两个方法来指定Tab 和 TanContent。
  TabSpec实例 通过  .newTabSpec(String tag )来创建。实例化后对其属性进行设置 。setIndicator()设置tab,它有3个重载的函数
  •  public TabHost.TabSpec setIndicatior(CharSwquence label,Drawable icon).指定tab的标题和图标。
  • public TabHost.TabSpec (View view)通过View来自定义tab
  • public TabHost.TabSpec(CharSequence label) 指定tab的标题,此时无图标。
   setContent 指定tab的展示内容,它也有3种重载
  • public TabHost.TabSpec setContent(TabHost.TabContentFactory )
  • public TabHost.TabSpec setContent(int ViewId)
  • public TabHost.TabSpec setContent(Intent intent)
  后两种方法比较后理解一个是通过 ViewId指定显示的内容,如.setContent(R.id.Team_EditText)。第三种则是直接通过Intent加载
一个新的Activity页。如.setContent(new Intent(this, MeetingActivity.class)));
  本例中是通过 TabContentFactory 来指定对应的TabContent。 TabContentFactory 是一个接口,其只包含了 一个返回 View 的
createTabContent(String tag)方法。

                
                
private TabContentFactory createMeetingDetails(Team team2) {

return new TabHost.TabContentFactory() {

@Override
public View createTabContent(String tag) {
          //设置View
setStatsTabContent();
return findViewById(R.id.teamStats);
}
};
}

private TabHost.TabContentFactory createMeetingList()
{
return new TabHost.TabContentFactory() {

@Override
public View createTabContent(String tag) {
      
meetingListAdapter = createMeetingListAdapter();
meetingList.setAdapter(meetingListAdapter);
return meetingList;
}
};
}
--------------------------------------------------------------------------------------------------------------------------------------
TabActivity 首先Android 里面有个名为TabActivity来给我们方便使用。其中有以下可以关注的函数:
public TabHost getTabHost ()   获得当前TabActivity的TabHost
  public TabWidget getTabWidget ()  获得当前TabActivity 的TabWidget

  public void setDefaultTab (String tag)  这两个函数很易懂, 就是设置默认的Tab
public void setDefaultTab (int index)   通过tab名——tag或者index(从0开始)

  protected void onRestoreInstanceState (Bundle state) 这 两个函数的介绍可以
  protected void onSaveInstanceState (Bundle outState) 参考 Activity的生命周期

Tabhost  那么我们要用到的Tab载体是TabHost,需要从TabActivity.getTabHost获取。
  现在看看TabHost类,它有3个内嵌类:1个类TabHost.TabSpec,2个接口 TabHost.TabContentFactory和
TabHost.OnTabChangeListener。后面会介绍这些类和接口。

TabHost类的一些函数:

  public void addTab (TabHost.TabSpec tabSpec) 添加 tab,参数TabHost.TabSpec通过下面的函数返回得到
  public TabHost.TabSpec newTabSpec (String tag) 创 建TabHost.TabSpec

  public void clearAllTabs () remove所有的Tabs
  public int getCurrentTab ()
  public String getCurrentTabTag ()
  public View getCurrentTabView ()
  public View getCurrentView ()
  public FrameLayout getTabContentView () 返回Tab content的FrameLayout

public TabWidget getTabWidget ()
  public void setCurrentTab (int index)       设置当前的Tab by index
  public void setCurrentTabByTag (String tag) 设置当前的Tab by tag
  public void setOnTabChangedListener (TabHost.OnTabChangeListener l) 设置TabChanged事件的响应处理
  public void setup () 这个函数后面介绍

  TabHost.TabSpec  从上面的函数可以知道如何添加tab了,要注意,这里的Tag(标签),不是Tab按钮上的文字。
  而要设置tab的label和content,需要设置TabHost.TabSpec类。 引用SDK里面的话——“A tab has a tab indicator, content, and a tag that is used to keep track of it.”,TabHost.TabSpec就是管理这3个东西:
  public String getTag ()
  public TabHost.TabSpec setContent
  public TabHost.TabSpec setIndicator

  我理解这里的Indicator 就是Tab上的label,它可以设置label : setIndicator (CharSequence label)
  或者同时设置label和icon :setIndicator (CharSequence label, Drawable icon)
  或者直接指定某个view : setIndicator (View view)

  对于Content ,就是Tab里面的内容,可以根据View的id把View作为内容 : setContent(int viewId)
  或者TabHost.TabContentFactory 的createTabContent(String tag)来处理:setContent(TabHost.TabContentFactory contentFactory)
  或者用new Intent 来引入其他Activity的内容:setContent(Intent intent)


主程序代码

Acitvit里面的代码代码



  • package com.yang.tabletest;  
  •   
  • import android.app.TabActivity;  
  • import android.os.Bundle;  
  • import android.view.LayoutInflater;  
  • import android.widget.TabHost;  
  •   
  • public class TableTestAcitivity extends TabActivity{  
  •     /** Called when the activity is first created. */  
  •     @Override  
  •     public void onCreate(Bundle savedInstanceState) {  
  •         super.onCreate(savedInstanceState);  
  •         //setContentView(R.layout.main);  
  •          
  •         //获得当前TabActivity的TabHost  
  •        TabHost tabHost = getTabHost();   
  •                   
  •                LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);   
  •          
  •                 tabHost.addTab(tabHost.newTabSpec("tab1")   
  •                          .setIndicator("主页")  
  •                          .setContent(R.id.view1));  
  •                            
  •                   
  •                  tabHost.addTab(tabHost.newTabSpec("tab2")   
  •                          .setIndicator("标题")   
  •                          .setContent(R.id.view2));   
  •                  tabHost.addTab(tabHost.newTabSpec("tab3")   
  •                          .setIndicator("简介")   
  •                          .setContent(R.id.view3));  
  •                  tabHost.addTab(tabHost.newTabSpec("tab4")   
  •                          .setIndicator("关于")   
  •                          .setContent(R.id.view4));  
  •     }  
  •   
  •       
  • }  





tabls.xml里面的代码

Tabi.xml代码



  • <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  •     android:layout_width="fill_parent"
  •     android:layout_height="fill_parent">   
  •    
  •     <TextView android:id="@+id/view1"
  •         android:background="@drawable/blue"
  •         android:layout_width="fill_parent"
  •         android:layout_height="fill_parent"
  •         android:text="@string/tabs_1_tab_1"/>   
  •    
  •     <TextView android:id="@+id/view2"
  •         android:background="@drawable/red"
  •         android:layout_width="fill_parent"
  •         android:layout_height="fill_parent"
  •         android:text="@string/tabs_1_tab_2"/>   
  •    
  •     <TextView android:id="@+id/view3"
  •         android:background="@drawable/green"
  •         android:layout_width="fill_parent"
  •         android:layout_height="fill_parent"
  •         android:text="@string/tabs_1_tab_3"/>   
  •          
  •     <TextView android:id="@+id/view4"
  •       android:background="@drawable/green"
  •       android:layout_width="fill_parent"
  •       android:layout_height="fill_parent"
  •       android:text="@string/tabs_1_tab_4"/>   
  •   
  • </FrameLayout>   


string.xml的代码

Java代码



  • <?xml version="1.0" encoding="utf-8"?>  
  • <resources>  
  •     <string name="hello">Hello World, TableTestAcitivity!</string>  
  •     <string name="app_name">阿福学习</string>  
  •     <string name="tabs_1_tab_1">主页</string>  
  •     <string name="tabs_1_tab_2">标题</string>  
  •     <string name="tabs_1_tab_3">关于</string>  
  •     <string name="tabs_1_tab_4">返回</string>  
  • </resources>  


color.xml代码

Java代码



  • <?xml version="1.0" encoding="utf-8"?>  
  • <resources>  
  •   <drawable name="darkgray">#404040ff</drawable>  
  •    
  •   <drawable name="red">#ff00ff</drawable>  
  •   <drawable name="green">#0ff0ff</drawable>  
  •   <drawable name="lightgray">#c0c0c0ff</drawable>  
  •    
  •   <drawable name="yellow">#ffFF33ff</drawable>  
  •   <drawable name="blue">#00ffff</drawable>  
  •   <drawable name="gray">#808080ff</drawable>  
  •   <drawable name="magenta">#ff6699ff</drawable>  
  •   <drawable name="cyan">#66ffffff</drawable>  
  •   <drawable name="black">#000000</drawable>   
  •   <drawable name="white">#FFFFFF</drawable>   
  • </resources>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值