There are two ways to implement tabhost. The first method inherits tabactivity and obtains tabhost from tabactivity using the gettabhost () method. The content of each tab can be defined in the layout file.
Mainactivity. xml
Private tabhost mytabhost;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Setcontentview (R. layout. Main );
Mytabhost = This. gettabhost ();
Layoutinflater. From (this). Inflate (R. layout. Main,
Mytabhost. gettabcontentview (), true );
Mytabhost. addtab (Mytabhost
. Newtabspec ("Tab 1 ")
. Setindicator ("Tab 1 ",
Getresources (). getdrawable (R. drawable. img01 ))
. Setcontent (R. Id. ll01 ));
Mytabhost. addtab (mytabhost
. Newtabspec ("Tab 2 ")
. Setindicator ("Tab 2 ",
Getresources (). getdrawable (R. drawable. img02 ))
. Setcontent (R. Id. ll01 ));
Mytabhost. addtab (mytabhost
. Newtabspec ("tab 3 ")
. Setindicator ("tab 3 ",
Getresources (). getdrawable (R. drawable. img03 ))
. Setcontent (R. Id. ll03 ));
}
Tab content layout file:
<? XML version = "1.0" encoding = "UTF-8"?>
<Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Linearlayout Android: Id = "@ + ID/ll01" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" Android: gravity = "center_horizontal"
Android: Orientation = "vertical">
<Edittext Android: Id = "@ + ID/widget34" Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" Android: text = "edittext"
Android: textsize = "18sp">
</Edittext>
<Button Android: Id = "@ + ID/widget30" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = "button">
</Button>
</Linearlayout>
<Linearlayout Android: Id = "@ + ID/ll02" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" Android: gravity = "center_horizontal"
Android: Orientation = "vertical">
<Analogclock Android: Id = "@ + ID/widget36"
Android: layout_width = "wrap_content" Android: layout_height = "wrap_content">
</Analogclock>
</Linearlayout>
<Linearlayout Android: Id = "@ + ID/ll03" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" Android: gravity = "center_horizontal"
Android: Orientation = "vertical">
<Radiogroup Android: Id = "@ + ID/widget43"
Android: layout_width = "166px" Android: layout_height = "98px"
Android: Orientation = "vertical">
<Radiobutton Android: Id = "@ + ID/widget44"
Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"
Android: text = "radiobutton">
</Radiobutton>
<Radiobutton Android: Id = "@ + ID/widget45"
Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"
Android: text = "radiobutton">
</Radiobutton>
</Radiogroup>
</Linearlayout>
</Framelayout>
The second method does not inherit tabactivity. You can define tabhost in the layout file, but the ID of tabwidget must be @ Android: ID/tabs. the ID of framelayout must be @ Android: ID/tabcontent.
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/hometabs"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Tabhost Android: Id = "@ + ID/tabhost"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Linearlayout
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Tabwidget Android: Id = "@ Android: ID/tabs"
Android: Orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
</Tabwidget>
<Framelayout Android: Id = "@ Android: ID/tabcontent"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
<Textview Android: Id = "@ + ID/view1"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" Android: text = "tab1"/>
<Textview Android: Id = "@ + ID/view2"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" Android: text = "tab2"/>
<Textview Android: Id = "@ + ID/view3"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" Android: text = "tab3"/>
</Framelayout>
</Linearlayout>
</Tabhost>
</Linearlayout>
Mainactivity
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Tabhost = (tabhost) findviewbyid (R. Id. tabhost );
Tabhost. Setup ();
Tabwidget = tabhost. gettabwidget ();
tabhost. addtab (tabhost
. newtabspec ("tab1")
. setindicator ("tab1",
getresources (). getdrawable (R. drawable. img01)
. setcontent (R. id. view1);
tabhost. addtab (tabhost
. newtabspec ("tab2")
. setindicator ("tab2",
getresources (). getdrawable (R. drawable. img02)
. setcontent (R. id. view2);
tabhost. addtab (tabhost
. newtabspec ("tab3")
. setindicator ("tab3",
getresources (). getdrawable (R. drawable. img03)
. setcontent (R. id. view3);