Android:tabhost Implementing TAB switching

Source: Internet
Author: User

Tabhost is the entire tab container, containing the tabwidget and framelayout two parts, Tabwidget is the expression of each tab, Framelayout is the tab content.

>> Downloads

There are two ways of implementing this:

1. Inheriting tabactivity

2. Inheriting Activity class

Method One: Inherit Tabactivity

Get Tabhost from tabactivity with the Gettabhost () method and set the label contents

Layout:

1, tabhost must be set Android:id to @android:id/tabhost
2, Tabwidget must be set Android:id to @android:id/tabs
3, framelayout must be set Android:id to @android:id/tabcontent

<?XML version= "1.0" encoding= "Utf-8"?><Tabhostxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" android:id= "@android: Id/tabhost"      >    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"    >        <TabwidgetAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content" android:id= "@android: Id/tabs"         ></Tabwidget>        <FramelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "1" android:id= "@android: id/tabcontent"         >        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/widget_layout_red"Android:background= "#ff0000"android:orientation= "vertical"            ></LinearLayout>                 <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/widget_layout_yellow"Android:background= "#FCD209"android:orientation= "vertical"            ></LinearLayout>            </Framelayout></LinearLayout></Tabhost>

Inherit tabactivity

 Public classMainactivityextendstabactivity {PrivateTabhost Tabhost; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.demo); //get tab tabhost from TabactivityTabhost =Gettabhost (); Tabhost.addtab (Tabhost//Create a new label one. Newtabspec ("one")                //Set label title. Setindicator ("Red")                //set the layout content of the label. SetContent (r.id.widget_layout_red)); Tabhost.addtab (Tabhost.newtabspec ("Setindicator" ("Yellow").). SetContent (R.id.widget_layout_yellow)); }}

The method by which the label is created:

Tabhost.addtab (tabhost. Newtabspec ("one"). Setindicator ("Red"). SetContent (r.id.widget_layout_red));

can also be split to write:

Tabhost.tabspec tab1 = Tabhost.newtabspec ("one"); Tab1.setindicator ("Red"); Tab1.setcontent (r.id.widget_layout_red);        Tabhost.addtab (TAB1);

Preview:

Click on the "Yellow" tab

Click on the "Red" tab

Method Two: Inherit the Activity class

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.