Http://www.javaeye.com/topic/465371
To create a tabhost in Main. xml or a custom *. xml file, two elements tabwidget and framelayout are required to put the two elements in linearlayout. Framelayout is used to change the content.
Note: tabwidget and framelayout have different ID namespaces: Android: Id = "@ Android: ID/idnames". This is required, so tabhost can be automatically found. The activity must inherit tabactivity.
Tabhost control-> tabwidget (must be named tabs)-> framelayout (must be named tabcontent ).
- <? XMLVersion = "1.0" encoding = "UTF-8"?>
- <TabhostXmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: Id = "@ Android: ID/tabhost"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <Linearlayout
- Android: Orientation = "vertical"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <Tabwidget
- Android: Id = "@ Android: ID/tabs"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"/>
- <Framelayout
- Android: Id = "@ Android: ID/tabcontent"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <Textview
- Android: Id = "@ + ID/textview1"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: text = "this is a tab"/>
- <Textview
- Android: Id = "@ + ID/textview2"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: text = "this is another tab"/>
- <Textview
- Android: Id = "@ + ID/textview3"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: text = "this is a third tab"/>
- </Framelayout>
- </Linearlayout>
- </Tabhost>
Public ClassHellotabwidgetExtendsTabactivity
- Public VoidOncreate (bundle savedinstancestate ){
- Super. Oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Mtabhost = gettabhost ();
- Mtabhost. addtab (mtabhost. newtabspec ("tab_test1"). setindicator ("Tab 1"). setcontent (R. Id. textview1 ));
- Mtabhost. addtab (mtabhost. newtabspec ("tab_test2"). setindicator ("Tab 2"). setcontent (R. Id. textview2 ));
- Mtabhost. addtab (mtabhost. newtabspec ("tab_test3"). setindicator ("tab 3"). setcontent (R. Id. textview3 ));
- Mtabhost. setcurrenttab (0 );
- }