Android------tabhost simple to use

Source: Internet
Author: User

Components used in conjunction with Tabhost:
Tabwidget: The tab bar representing the tabs
Tabspec: Represents a tab page for tabs


Tabhost is just a simple container that provides two ways to create, add a tab

Newtabspec (String tag): Create tab
AddTab (Tabhost.tabspec tabspec): Add tab


Steps:

1. Define the Tabhost component in the interface layout file and define the contents of the tab for that component

2.Activity Inheritance Tabactivity

3. Call Tabactivity's Gettabhost () method to get the Tabhost object

4. Create and Add tabs using the Tabhost object method

Layout file:

<?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=" match_parent "android:layout_height=" Match_parent "an droid:layout_weight= "1" > <linearlayout android:layout_width= "match_parent" android:layout_height= "            Match_parent "android:orientation=" vertical "> <tabwidget android:id=" @android: Id/tabs "            Android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> <framelayout Android:id= "@android: Id/tabcontent" android:layout_width= "Match_parent" Android:layout_hei ght= "Match_parent" > <!--define the contents of the first tab--<linearlayout android:id= "@+id/                Tab01 "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " android:orientation= "Vertical" &Gt <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content             "Android:text=" Tab1 "android:textsize=" 11pt "/> </LinearLayout> <!--define the contents of the Second tab--<linearlayout android:id= "@+id/tab02" a Ndroid:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "V Ertical "> <textview android:layout_width=" wrap_content "Androi            d:layout_height= "Wrap_content" android:text= "tab2" android:textsize= "11pt"/> </LinearLayout> <!--Define the contents of the Third tab--<linearlayout android:id=              "@+id/tab03" android:layout_width= "fill_parent" android:layout_height= "Fill_parent"  android:orientation= "vertical" android:textsize= "11pt" > <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:t    ext= "Tab3" android:textsize= "11pt"/> </LinearLayout> </FrameLayout> </LinearLayout></TabHost>


The Tabhost container requires a combination of two components: Tabwidget and Framelayout, where the header bar of the Tabwidget definition tab
Framelayout is used to cascade multiple options interfaces

Activity:

public class Mainactivity extends tabactivity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Get the Tabhost component in the activity Tabhost Tabhost = Gettabhost ();//Create the first tab page Tabspec TAB1 = Tabhost.newtabspec ("Tab1"). Setindicator ("01")//Set caption. setcontent ( R.ID.TAB01); Set content//Add first tab Tabhost.addtab (TAB1); Tabspec tab2 = Tabhost.newtabspec ("tab2")//Place the icon on the label header. Setindicator ("", Getresources (). Getdrawable (R.drawable.ic_ Launcher). SetContent (R.ID.TAB02);//Add a second tab tabhost.addtab (TAB2); Tabspec tab3 = Tabhost.newtabspec ("Tab3"). Setindicator ("the"). SetContent (R.ID.TAB03);//Add a Third tab Tabhost.addtab (TAB3 );}}


Sometimes the tabs are below, just modify the layout file to

Layout file:

<?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=" match_parent "android:layout_height=" Match_parent "an droid:layout_weight= "1" > <relativelayout android:layout_width= "match_parent" android:layout_height            = "Match_parent" android:orientation= "vertical" > <tabwidget android:id= "@android: Id/tabs" Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:layout_al Ignparentbottom= "true"/> <framelayout android:id= "@android: Id/tabcontent" Android:layo  Ut_width= "Match_parent" android:layout_height= "match_parent" android:layout_above= "@android: Id/tabs"                > <!--define the contents of the first tab--<linearlayout android:id= "@+id/tab01" Android:layout_width= "Fill_paRent "android:layout_height=" fill_parent "android:orientation=" vertical "> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_conten T "android:text=" TAB1 "android:textsize=" 11pt "/> </linearlayout&gt            ; <!--define the contents of the Second tab--<linearlayout android:id= "@+id/tab02" android:layou T_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical"                ; <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content             "Android:text=" tab2 "android:textsize=" 11pt "/> </LinearLayout>           <!--define the contents of the Third tab--<linearlayout android:id= "@+id/tab03"     Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" Android:orientat ion= "vertical" android:textsize= "11pt" > <textview android:layout_w                    Idth= "Wrap_content" android:layout_height= "wrap_content" android:text= "Tab3" Android:textsize= "11pt"/> </LinearLayout> </FrameLayout> </relativelay Out></tabhost>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android------tabhost simple to use

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.