First, define the Tabhost layout file:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<tabhost xmlns: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_alignparentbottom= "true"
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"/>
</LinearLayout>
</TabHost>
Where Tabwidget is the label above the tab, Framelayout is the contents of the tab.
This is defined in the Java class file as follows:
Copy Code code as follows:
public class Mainactivity extends Tabactivity {
Private Tabhost My_tabhost;
Private Tabwidget My_tabwidget;
private int i,k;
Private TextView TV;
Private string[] Tabmenu = {"System", "hardware", "Operation"};
Private Intent intent0, Intent1, Intent2;
Private intent[] intents = {intent0, intent1, intent2};
Private Tabhost.tabspec tabSpec0, TABSPEC1, TABSPEC2, TABSPEC3;
Private tabhost.tabspec[] Tabspecs = {tabSpec0, tabSpec1, TABSPEC2, TABSPEC3};
public static context Mcontext;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Do not have a form title
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_main);
Setcontentview (R.layout.activity_main);
My_tabhost = Gettabhost ();
intent0 = new Intent (this, system.class);
Intent1 = new Intent (this, hardware.class);
Intent2 = new Intent (this, operation.class);
TABSPEC0 = My_tabhost.newtabspec ("System"). Setindicator (Tabmenu[0],null).
SetContent (INTENT0);
TABSPEC1 = My_tabhost.newtabspec ("Hardware"). Setindicator (Tabmenu[1],null).
SetContent (INTENT1);
TABSPEC2 = My_tabhost.newtabspec ("operation"). Setindicator (Tabmenu[2],null).
SetContent (Intent2);
My_tabhost.addtab (TABSPEC1);
My_tabhost.addtab (TABSPEC0);
My_tabhost.addtab (TABSPEC2);
<br>//Set the default selected tab to 2nd
My_tabhost.setcurrenttab (1);
}
}
Each tab corresponds to a intent, each of which corresponds to a class, and the corresponding class is displayed when the tab is selected. Intent
The results of the operation are as follows: