There are two ways to implement Tabhost, the first to inherit tabactivity, and to get tabhost from tabactivity with the Gettabhost () method. The contents of each tab are 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 way, do not inherit tabactivity, define Tabhost in the layout file, but the ID of tabwidget must be @android:id/tabs,framelayout ID 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 = (tabhost) Findviewbyid (r.id.tabhost);
Tabhost.setup ();
Tabwidget 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));
Android's Tabhost