This example introduces TabActivity. In the new Android version, TabActivity is not recommended, but Fragment. TabActivity provides a Tab Control. The key class is TabHost and TabHost is the View of TabActivity. You can use getTabHost () to obtain the TabHost corresponding to TabActivity.
You can use TabHost. tabSpec is used to add a page to the Tab window. TabSpec is used to describe a page (Tab). Each Tab can have a tag to distinguish a page in the Tab window, you can use text, icons, etc. to display) and Content (page Content ).
Content is constructed using the View resource Id, TabHost. TabContentFactory, or an Intent instance (the Intent can start an Activity ). This is why TabActivity is an ActivityGroup.
In this example, the corresponding code Tab1.java uses the View resource ID method to set the Content of the Tab:
[Java]
TabHost tabHost = getTabHost ();
LayoutInflater. from (this). inflate (R. layout. tabs1,
TabHost. getTabContentView (), true );
TabHost. addTab (tabHost. newTabSpec ("tab1 ")
. SetIndicator ("tab1 ")
. SetContent (R. id. view1 ));
TabHost. addTab (tabHost. newTabSpec ("tab3 ")
. SetIndicator ("tab2 ")
. SetContent (R. id. view2 ));
TabHost. addTab (tabHost. newTabSpec ("tab3 ")
. SetIndicator ("tab3 ")
. SetContent (R. id. view3 ));
TabHost tabHost = getTabHost ();
LayoutInflater. from (this). inflate (R. layout. tabs1,
TabHost. getTabContentView (), true );
TabHost. addTab (tabHost. newTabSpec ("tab1 ")
. SetIndicator ("tab1 ")
. SetContent (R. id. view1 ));
TabHost. addTab (tabHost. newTabSpec ("tab3 ")
. SetIndicator ("tab2 ")
. SetContent (R. id. view2 ));
TabHost. addTab (tabHost. newTabSpec ("tab3 ")
. SetIndicator ("tab3 ")
. SetContent (R. id. view3 ));
Note that you must use the View specified in setContent to expand them to tabHost. getTabContentView.
R. layout. tabs1.xml is defined as follows:
[Html]
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<TextView android: id = "@ + id/view1 ″
Android: background = "@ drawable/blue"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_sharetab_1"/>
<TextView android: id = "@ + id/view2 ″
Android: background = "@ drawable/red"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_1_tab_2"/>
<TextView android: id = "@ + id/view3 ″
Android: background = "@ drawable/green"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_1_tab_3"/>
</FrameLayout>
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<TextView android: id = "@ + id/view1 ″
Android: background = "@ drawable/blue"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_sharetab_1"/>
<TextView android: id = "@ + id/view2 ″
Android: background = "@ drawable/red"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_1_tab_2"/>
<TextView android: id = "@ + id/view3 ″
Android: background = "@ drawable/green"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_1_tab_3"/>
</FrameLayout>