Switch to the "Go" tab

Source: Internet
Author: User

Reprint Address: http://www.cnblogs.com/vir56k/archive/2011/07/04/2097570.html

Believe that friends who have used Android phones have seen tabs like the following, this article we try to do demo.

This tab page, or tag card. is divided into two parts:

One of the top buttons (clickable) of the Switch card section

The primary display area of a main content area (which displays the "second form" font).

The effect we want to achieve is to click on the tab portion of the Toggle tab, and the contents of the main display area change. So let's look at the page layout code

<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">

<TabwidgetAndroid:id= "@android: Id/tabs"
Android:layout_width= "Fill_parent"
Android:layout_height= "Wrap_content"
Android:paddingleft= "1dip"
Android:paddingright= "1dip"
Android:paddingtop= "4dip"
/>

<FramelayoutAndroid:id= "@android: Id/tabcontent"
Android:layout_width= "Fill_parent"
Android:layout_height= "1dip"
Android:layout_weight= "1"
/>
</linearlayout>
</tabhost>

As shown in the code above:

The entire form is under a tabhost element. Tabhost is the root element. He contains a sub-layout object LinearLayout, the layout object's direction is "vertical", note this vertical direction. It indicates how to arrange the sub-object of this layout object, that is, it indicates the arrangement of the two controls, Tabwidget and Framelayout. Our present arrangement is tabwidget on, framelayout under. If you want to implement the tab label effect at the bottom, try the relativelayout.

Tabwidget is the tag object. Is the top tab that you use to switch. Note The ID must be @android: Id/tabs

Framelayout is the content area where the contents change as the tag card changes. Note The ID must be @android: id/tabcontent

After the layout has been built. The next step is to add a sub-tab to the layout.

First, let our activty inherit from Tabactivity.

public class ActTabActivityDemo1 extends tabactivity

Get Tabhost objects when OnCreate, and Add tabs

_tabhost = gettabhost (); AddTabPage1 ();//Perform the method of adding a sub-tab

Let's look at the concrete implementation of the AddTabPage1 () method.

Intent Internt1=NewIntent ();
Internt1.setclass (This, Act1.Class);

Tabspec Tabspec=_tabhost.newtabspec ("act1);
        // Specifies the display name of the tab
         tabspec.setindicator ( " tab one ");
        // specify jump direction
         tabspec.setcontent (INTERNT1);           
        _tabhost.addtab (TabSpec);

As shown in the code above, we have constructed a Tabspec object and called _tabhost.addtab (Tabspec) to add the object to the tab collection.

Tabspec is a tab object, or Tabspec describes a tab. Pass

Tabspec.setindicator the display name of the specified tab.

Tabspec.setcontent (INTERNT1); Specifies the jump direction, where the intent object is executed when the when top portion of the label card is specified. We often use intent to make jumps between forms.

Once you have completed the above steps, you can specifically implement the layout (content) of the specific tab.

The completed activity code is posted below

PackageDemo. TabActivityDemo1;

Importandroid.app.Activity;
Importandroid.app.TabActivity;
ImportAndroid.content.Intent;
ImportAndroid.os.Bundle;
ImportAndroid.widget.TabHost;
ImportAndroid.widget.TabHost.TabSpec;

PublicClassActTabActivityDemo1Extendstabactivity {
PrivateTabhost _tabhost;

/**Called when the activity is first created.*/
@Override
PublicvoidOnCreate (Bundle savedinstancestate) {
Super. OnCreate (Savedinstancestate);
Setcontentview (R.layout.main);

_tabhost=Gettabhost ();

AddTabPage1 ();

AddTabPage2 ();

AddTabPage1 ();
}

PrivatevoidAddTabPage1 () {
//TODO auto-generated Method Stub

Intent Internt1=NewIntent ();
Internt1.setclass (This, Act1.Class);

Tabspec Tabspec=_tabhost.newtabspec ("Act1");
//Specify the display name of the tab
Tabspec.setindicator ("TAB One");
//Specify Jump Direction
Tabspec.setcontent (INTERNT1);
_tabhost.addtab (TABSPEC);
}

PrivatevoidAddTabPage2 () {
//TODO auto-generated Method Stub

Intent Internt1=NewIntent ();
Internt1.setclass (This,act2. class);
        
         Tabspec tabspec = _ Tabhost.newtabspec ( "act2 ");
        tabspec.setindicator ( " tab two "         tabspec.setcontent (INTERNT1);           
        _tabhost.addtab (TABSPEC);
    }
}


Full code Download

Switch to the "Go" tab

Related Article

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.