1. create four tab styles for 9patch. For details, refer to the default Android resource.
Tab_unselected.9.png tab_selected.9.png tab_press.9.png tab_focus.9.png
These four resources represent the four statuses of the tab.
2. Define the selector style (tab_indicator.xml) of the tab and put it in the drawable folder. The Code is as follows:
1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3 <!-- Non focused states -->
4 <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
5 <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />
6 <!-- Focused states -->
7 <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
8 <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
9 <!-- Pressed -->
10 <item android:state_pressed="true" android:drawable="@drawable/tab_press" />
11 </selector>
3. Compile the indicator layout file (also called tab_indicator.xml) and put it in the layout folder. The Code is as follows:
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="0dip"
4 android:layout_height="64dip"
5 android:layout_weight="1"
6 android:layout_marginLeft="-3dip"
7 android:layout_marginRight="-3dip"
8 android:orientation="vertical"
9 android:background="@drawable/tab_indicator">
10 <ImageView android:id="@+id/icon"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:layout_centerHorizontal="true"
14 />
15 <TextView android:id="@+id/title"
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content"
18 android:layout_alignParentBottom="true"
19 android:layout_centerHorizontal="true"
20 style="?android:attr/tabWidgetStyle" mce_style="?android:attr/tabWidgetStyle"
21 />
4. Next we will use our own tab style in tabactivity:
1 // obtain tabwidget first
2 mtabhost = gettabhost ();
3 linearlayout LL = (linearlayout) mtabhost. getchildat (0 );
4 tabwidget Tw = (tabwidget) ll. getchildat (0 );
Then, use the code similar to the following to create tabspec.
1 RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_indicator, tw, false);
2 TextView tvTab1 = (TextView)tabIndicator1.getChildAt(1);
3 tvTab1.setText("tab1");
4 mTabHot = mTabHost.newTabSpec("TAB_1")
5 .setIndicator(tabIndicator1)
6 .setContent(contentIntent);