How to use the Android Tabhost (tab) _android

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.