1. create four tab styles for 9patch. For details, refer to the default Android resource.
Tab_unselected.9.png tab_selected.9.pngtab_press.9.pngtab_focus.9.png
These four resources represent the four statuses of the tab.
2. Define the selector style of the tab (called tab_indicator.xml) and put it in the drawable folder,CodeAs follows:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Selector Xmlns: Android = "Http://schemas.android.com/apk/res/android" >
<! -- Non focused states -->
< Item Android: state_focused = "False" Android: state_selected = "False" Android: state_pressed = "False" Android: drawable = "@ Drawable/tab_unselected" />
< Item Android: state_focused = "False" Android: state_selected = "True" Android: state_pressed = "False" Android: drawable = "@ Drawable/tab_selected" />
<! -- Focused states -->
< Item Android: state_focused = "True" Android: state_selected = "False" Android: state_pressed = "False" Android: drawable = "@ Drawable/tab_focus" />
< Item Android: state_focused = "True" Android: state_selected = "True" Android: state_pressed = "False" Android: drawable = "@ Drawable/tab_focus" />
<! -- Pressed -->
< Item Android: state_pressed = "True" Android: drawable = "@ Drawable/tab_press" />
</ Selector >
3. Compile the indicator layout file (also called tab_indicator.xml) and put it in the layout folder. The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Relativelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "0dip"
Android: layout_height = "64dip"
Android: layout_weight = "1"
Android: layout_marginleft = "-3dip"
Android: layout_marginright = "-3dip"
Android: Orientation = "Vertical"
Android: Background = "@ Drawable/tab_indicator" >
< Imageview Android: ID = "@ + ID/icon"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: layout_centerhorizontal = "True"
/>
< Textview Android: ID = "@ + ID/Title"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: layout_alignparentbottom = "True"
Android: layout_centerhorizontal = "True"
Style = "? Android: ATTR/tabwidgetstyle" Mce_style = "? Android: ATTR/tabwidgetstyle"
/>
4. Next we will use our own tab style in tabactivity:
// Obtain the tabwidget first
Mtabhost = gettabhost ();
Linearlayout LL = (linearlayout) mtabhost. getchildat (0 );
Tabwidget Tw = (tabwidget) ll. getchildat (0 );
Then, use the code similar to the following to create tabspec.
Relativelayout tabindicator1 = (relativelayout) layoutinflater. From (this). Inflate (R. layout. tab_indicator, TW, false );
Textview tvtab1 = (textview) tabindicator1.getchildat (1 );
Tvtab1.settext ("tab1 ");
Mtabhot = mtabhost. newtabspec ("tab_1 ")
. Setindicator (tabindicator1)
. Setcontent (contentintent );