Android中的TabActivity和自定义tab页

来源:互联网 发布:台湾现状知乎 编辑:程序博客网 时间:2024/05/23 00:01

近日,同事在做tab页相关的东西,我借此机会整理学习了以下tab页的自定义~


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 : setContent(int viewId)
  或者TabHost.TabContentFactory 的createTabContent(String tag)来处理:setContent(TabHost.TabContentFactory contentFactory)
  或者用new Intent 来引入其他Activity的内容:setContent(Intent intent)

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


TabHost localTabHost = getTabHost();localTabHost.addTab(buildTabSpec("message_tab", R.string.main_news,R.drawable.icon_2_n, this.mInfoIntent));public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {switch (buttonView.getId()) {case R.id.radio_button1:this.mHost.setCurrentTabByTag("message_tab");break;case R.id.radio_button2:this.mHost.setCurrentTabByTag("userinfo_tab");break;case R.id.radio_button3:this.mHost.setCurrentTabByTag("search_tab");break;}}}private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,final Intent content) {return this.mHost.newTabSpec(tag).setIndicator(getString(resLabel),getResources().getDrawable(resIcon)).setContent(content);}

TabActivity的布局文件,用RadioButton代替难看的tab页,实现自定义tab页,当然radio也要自定义才能实现效果,这里就不赘述RadioButton如何自定义了~

<?xml version="1.0" encoding="UTF-8"?><TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"  android:layout_height="fill_parent"  xmlns:android="http://schemas.android.com/apk/res/android">    <LinearLayout android:orientation="vertical"                  android:layout_width="fill_parent"                   android:layout_height="fill_parent">        <FrameLayout android:id="@android:id/tabcontent"                      android:layout_width="fill_parent"                      android:layout_height="0.0dip"                      android:layout_weight="1.0" />                   <!--gone 掉TabWidget中的tab-->        <TabWidget android:id="@android:id/tabs"                    android:visibility="gone"                    android:layout_width="fill_parent"                    android:layout_height="wrap_content"                    android:layout_weight="0.0" />        <RadioGroup android:gravity="center_vertical"                     android:layout_gravity="bottom"                     android:orientation="horizontal"                     android:id="@+id/main_radio"                                      android:layout_width="fill_parent"                     android:layout_height="wrap_content">                                            <RadioButton android:id="@+id/radio_button1"                          android:layout_marginTop="2.0dip"                          android:text="@string/main_news"                          android:drawableTop="@drawable/icon_2_n"                         style="@style/main_tab_bottom" />            <RadioButton android:id="@+id/radio_button2"                          android:layout_marginTop="2.0dip"                          android:text="@string/main_my_info"                          android:drawableTop="@drawable/icon_3_n"                         style="@style/main_tab_bottom"                          />            <RadioButton android:id="@+id/radio_button3"                          android:layout_marginTop="2.0dip"                          android:text="@string/menu_search"                          android:drawableTop="@drawable/icon_4_n"                         style="@style/main_tab_bottom" />                 </RadioGroup>    </LinearLayout></TabHost>