Tabhost is the entire tab container, containing the tabwidget and framelayout two parts, Tabwidget is the expression of each tab, Framelayout is the tab content.
>> Downloads
There are two ways of implementing this:
1. Inheriting tabactivity
2. Inheriting Activity class
Method One: Inherit Tabactivity
Get Tabhost from tabactivity with the Gettabhost () method and set the label contents
Layout:
1, tabhost must be set Android:id to @android:id/tabhost
2, Tabwidget must be set Android:id to @android:id/tabs
3, framelayout must be set Android:id to @android:id/tabcontent
<?XML version= "1.0" encoding= "Utf-8"?><Tabhostxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" android:id= "@android: Id/tabhost" > <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TabwidgetAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content" android:id= "@android: Id/tabs" ></Tabwidget> <FramelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "1" android:id= "@android: id/tabcontent" > <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/widget_layout_red"Android:background= "#ff0000"android:orientation= "vertical" ></LinearLayout> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/widget_layout_yellow"Android:background= "#FCD209"android:orientation= "vertical" ></LinearLayout> </Framelayout></LinearLayout></Tabhost>
Inherit tabactivity
Public classMainactivityextendstabactivity {PrivateTabhost Tabhost; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.demo); //get tab tabhost from TabactivityTabhost =Gettabhost (); Tabhost.addtab (Tabhost//Create a new label one. Newtabspec ("one") //Set label title. Setindicator ("Red") //set the layout content of the label. SetContent (r.id.widget_layout_red)); Tabhost.addtab (Tabhost.newtabspec ("Setindicator" ("Yellow").). SetContent (R.id.widget_layout_yellow)); }}
The method by which the label is created:
Tabhost.addtab (tabhost. Newtabspec ("one"). Setindicator ("Red"). SetContent (r.id.widget_layout_red));
can also be split to write:
Tabhost.tabspec tab1 = Tabhost.newtabspec ("one"); Tab1.setindicator ("Red"); Tab1.setcontent (r.id.widget_layout_red); Tabhost.addtab (TAB1);
Preview:
Click on the "Yellow" tab
Click on the "Red" tab
Method Two: Inherit the Activity class