According to the online materials, a general template-like class MyTabActivity is written to implement a TabActivity with Icon + text Label. To apply a class, you only need to add the Icon and Label to correspond to each Activity, you can create a TabActivity.
1. template class MyTabActivity. java (reusable)
[Java]
Package amao. callbye;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
Import android. app. Activity;
Import android. app. TabActivity;
Import android. content. Context;
Import android. content. Intent;
Import android. graphics. Color;
Import android. graphics. drawable. Drawable;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. Gravity;
Import android. view. Window;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. TabHost;
Import android. widget. TabHost. OnTabChangeListener;
Import android. widget. TabHost. TabSpec;
Import android. widget. TextView;
/**
* Abstract TabActivity with icon + text TabSpec support for each Activity
* Sub class need set "layout" and "selectDrawable" (tab selected background image) in constructor
* And implement getMyTabList () to add tab configuration
*
* @ Author Anderson Mao, 2012-05-01
*/
Public abstract class MyTabActivity extends TabActivity {
Private static String TAG_NAME = MyTabActivity. class. getSimpleName ();
Private TabHost tabHost;
Private int tabLayout;
Private int selectDrawable;
Private Drawable selectBackground;
Private int textColor = Color. GRAY;
Private int selectTextColor = Color. LTGRAY;
Private Map <String, TabView> tabViewMap = new HashMap <String, TabView> ();
Private String tabViewTagPrev = null;
Public abstract List <MyTab> getMyTabList ();
Public MyTabActivity (int tabLayout, int selectDrawable ){
This. tabLayout = tabLayout;
This. selectDrawable = selectDrawable;
}
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE );
SetContentView (tabLayout );
TabHost = getTabHost (); // Get TabHost after setContentView ()
//
InitTabHost ();
}
Private void initTabHost (){
SelectBackground = this. getResources (). getDrawable (selectDrawable );
Int index = 0;
// Create TabSpec for each MyTab. The first tab is the default
String defaultTag = null;
TabView defaultTabView = null;
List <MyTab> myTabList = getMyTabList ();
For (MyTab myTab: myTabList ){
Index ++;
String tag = Integer. toString (index );
TabView view = new TabView (this, myTab. icon, myTab. text );
TabSpec tabSpec = tabHost. newTabSpec (tag)
. SetIndicator (view)
. SetContent (new Intent (this, myTab. activity ))
;
TabViewMap. put (tag, view );
TabHost. addTab (tabSpec );
If (defaultTag = null ){
DefaultTag = tag;
DefaultTabView = view;
}
}
// Listener on tab change
TabHost. setOnTabChangedListener (new OnTabChangeListener (){
@ Override
Public void onTabChanged (String tabId ){
Log. d (TAG_NAME, "change tab: id =" + tabId + ", prevId =" + tabViewTagPrev );
If (tabViewTagPrev! = Null ){
// Reset prev tab
TabView tvPrev = tabViewMap. get (tabViewTagPrev );
If (tvPrev! = Null ){
TvPrev. setBackgroundDrawable (null );
TvPrev. textView. setTextColor (textColor );
}
}
// Set current selected tab
TabView TV = tabViewMap. get (tabId );
If (TV! = Null ){
TV. setBackgroundDrawable (selectBackground );
TV. textView. setTextColor (selectTextColor );
}
//
TabViewTagPrev = tabId;
}
});
// Set default tab
If (defaultTag! = Null ){
DefaultTabView. setBackgroundDrawable (selectBackground );
DefaultTabView. textView. setTextColor (selectTextColor );
TabViewTagPrev = defaultTag;
}
}
/**
* Layout for each TabSpec
*/
Private class TabView extends LinearLayout {
Private ImageView imageView;
Private TextView textView;
Public TabView (Context c, int icon, String text ){
Super (c );
SetOrientation (VERTICAL );
SetGravity (Gravity. CENTER );
ImageView = new ImageView (c );
ImageView. setImageDrawable (this. getResources (). getDrawable (icon ));
ImageView. setBackgroundColor (Color. TRANSPARENT );
AddView (imageView );
TextView = new TextView (c );
TextView. setText (text );
TextView. setTextColor (textColor );
TextView. setGravity (Gravity. CENTER );
AddView (textView );
}
}
/**
* Options for each TabSpec. Icon + Text + Activity
*/
Public class MyTab {
Private int icon;
Private String text;
Private Class <? Extends Activity> activity;
Public MyTab (int icon, String text, Class <? Extends Activity> activity ){
This. icon = icon;
This. text = text;
This. activity = activity;
}
}
}
2. Actual Application class CallbyeTabActivity. java (using MyTabActivity)
[Java]
Package amao. callbye;
Import java. util. ArrayList;
Import java. util. List;
/**
* TabActivity by configuration
* @ See MyTabActivity. java
*
* @ Author Anderson Mao, 2012-05-01
*/
Public class CallbyeTabActivity extends MyTabActivity {
Public CallbyeTabActivity (){
Super (R. layout. tab, R. drawable. icon_tab_select );
}
@ Override
Public List <MyTab> getMyTabList (){
List <MyTab> myTabList = new ArrayList <MyTab> ();
//
MyTabList. add (new MyTab (R. drawable. icon_home, getString (R. string. tab_home), CallbyeActivity. class ));
MyTabList. add (new MyTab (R. drawable. icon_setting, getString (R. string. tab_setting), SettingActivity. class ));
MyTabList. add (new MyTab (R. drawable. icon_help, getString (R. string. tab_help), HelpActivity. class ));
MyTabList. add (new MyTab (R. drawable. icon_history, getString (R. string. tab_info), InfoActivity. class ));
//
Return myTabList;
}
}
3. Layout (tab. xml)
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<TabHost android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ android: id/tabhost"
Xmlns: android = "http://schemas.android.com/apk/res/android"
>
<RelativeLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical"
Android: padding = "0dip"
>
<FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: id = "@ android: id/tabcontent"
>
</FrameLayout>
<TabWidget www.2cto.com
Android: layout_width = "fill_parent"
Android: layout_height = "50dip"
Android: id = "@ android: id/tabs"
Android: background = "@ android: color/black"
Android: layout_alignBottom = "@ android: id/tabcontent"
/>
</RelativeLayout>
</TabHost>
Author: andresonmao