1. Create three activity classes
Mainactiviey. Java
Package tianshuai. home_page; </P> <p> Import android. app. activity; <br/> Import android. app. activitygroup; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. tabhost; <br/> Import android. widget. tabhost. tabspec; </P> <p> public class mainactivity extends activitygroup implements view. onclicklistener <br/>{< br/> Public static tabhost tab_host; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> createtab (); <br/>}</P> <p> Public void createtab () <br/> {<br/> tab_host = (tabhost) findviewbyid (R. id. tab_host); <br/> tab_host.setup (this. getlocalactivitymanager (); </P> <p> tab_host.setonclicklistener (this); </P> <p> tabspec ts1 = tab_host.newtabspec ("cell phone site "); <br/> ts1.setindicator ("playing", getresources (). getdrawable (R. drawable. find); <br/> ts1.setcontent (new intent (this, cellphone_web.class); </P> <p> tab_host.addtab (ts1 ); </P> <p> tabspec ts2 = tab_host.newtabspec ("software game"); <br/> ts2.setindicator ("Local Database", getresources (). getdrawable (R. drawable. history); <br/> ts2.setcontent (new intent (this, software. class); </P> <p> tab_host.addtab (ts2); <br/> tab_host.setcurrenttab (1 ); <br/>}</P> <p> Public void onclick (view arg0) {<br/> throw new unsupportedoperationexception ("not supported yet. "); <br/>}</P> <p>}
2. cellphone_web.java and software. Java are empty classes that inherit activity.
3. Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <tabhost <br/> Android: id = "@ + ID/tab_host" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <linearlayout <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: padding = "5dp"> <br/> <framelayout <br/> Android: Id = "@ Android: ID/tabcontent "<br/> Android: layout_width =" fill_parent "<br/> Android: layout_height =" wrap_content "<br/> Android: padding = "5dp" <br/> Android: layout_weight = "1"/> <br/> <tabwidget <br/> Android: Id = "@ Android: ID/tabs "<br/> Android: layout_width =" fill_parent "<br/> Android: layout_height =" wrap_content "<br/> Android: layout_weight = "0"/> <br/> </linearlayout> <br/> </tabhost> <br/> </linearlayout> <br/>
Note:Tabwidget and framelayout have different ID namespaces Android: Id = "@ Android: ID/idnames". This is required. Therefore, tabhost can be automatically found. The activity must inherit tabactivity.
If you want the tab to be above:
<Framelayout <br/> Android: Id = "@ Android: ID/tabcontent" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: padding = "5dp" <br/> Android: layout_weight = "0"/> <br/> <tabwidget <br/> Android: Id = "@ Android: ID/tabs" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_weight = "1"/>If the content is above, tabwidget must change the position with framelayout.