For the final
Code structure
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Id = "@ + ID/hometabs" <br/> Android: orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <! -- Tabhost must contain a tabwidget and a framelayout --> <br/> <tabhost Android: Id = "@ + ID/tabhost" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> <linearlayout <br/> Android: orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <! -- The ID attribute of tabwidget must be @ Android: ID/Tabs --> <br/> <tabwidget Android: Id = "@ Android: ID/tabs" <br/> Android: orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> <br/> </tabwidget> <br/> <! -- The ID attribute of framelayout must be @ Android: ID/tabcontent --> <br/> <framelayout Android: Id = "@ Android: ID/tabcontent" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <textview Android: Id = "@ + ID/view1" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"/> <br/> <textview Android: id = "@ + ID/view2" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"/> <br/> <textview Android: Id = "@ + ID/view3" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"/> <br/> </framelayout> </P> <p> </linearlayout> <br/> </tabhost> <br/> </ linearlayout> <br/>
The Java code is as follows:
Package cn.com. taghost. test; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. tabhost; <br/> Import android. widget. tabwidget; </P> <p> public class taghosttest2 extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> // obtain the tabhost object <br/> tabhost = (tabhost) findviewbyid (R. id. tabhost); <br/> // If tabactivity is not inherited, use this method to load and start tabhost. <br/> tabhost. setup (); <br/> tabhost. addtab (tabhost. newtabspec ("tab1 "). setindicator ("first tag", <br/> getresources (). getdrawable (R. drawable. icon )). setcontent (<br/> r. id. view1); </P> <p> tabhost. addtab (tabhost. newtabspec ("tab3 "). setindicator ("third tag") <br/>. setcontent (R. id. view3); </P> <p> tabhost. addtab (tabhost. newtabspec ("tab2 "). setindicator ("second tag") <br/>. setcontent (R. id. view2); <br/>}< br/>}
Run the command to get the correct result.
Waste of words: note that
1. layout file format. And the ID attribute values of tabwidget and framelayout.
Second, tabwidget indicates the label part, and framelayout indicates the content part displayed after clicking the label. The components declared in framelayout indicate that they are qualified to be the content of the tag, but must be specified in the Code.
Do you also want this result. Display tags in the following section
You only need to modify the layout of Main. xml.
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Id = "@ + ID/hometabs" Android: orientation = "vertical" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <br/> <! -- Tabhost must contain a tabwidget and a framelayout --> <br/> <tabhost Android: Id = "@ + ID/tabhost" Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> <br/> <linearlayout Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> </P> <p> <! -- The ID attribute of framelayout must be @ Android: ID/tabcontent --> <br/> <framelayout Android: Id = "@ Android: ID/tabcontent" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <br/> <textview Android: Id = "@ + ID/view1" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: text = "Hello Baby! "<Br/> <textview Android: Id =" @ + ID/view2 "Android: layout_width =" fill_parent "<br/> Android: layout_height = "fill_parent"/> <br/> <textview Android: Id = "@ + ID/view3" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"/> <br/> </framelayout> <br/> <relativelayout Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <! -- The ID attribute of tabwidget must be @ Android: ID/Tabs --> <br/> <tabwidget Android: Id = "@ Android: ID/tabs" <br/> Android: orientation = "horizontal" Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_alignparentbottom = "true" <br/> Android: paddingbottom = "0dp" <br/> </tabwidget> <br/> </relativelayout> <br/> </linearlayout> <br/> </ tabhost> <br/> </linearlayout> <br/>
To keep the label at the bottom of the parent container unchanged, we use Android: layout_alignparentbottom = "true ", this attribute exists only in the relativelayout layout. This is why tabwidget is put into a relativelayout.
In addition, in the lineaerlayout layout, the positions of tabwidget and framelayout are changed.