Android Learning Note 10--tabhost

Source: Internet
Author: User

tabhost--Label page

= = "

Tabhost, you can place multiple tabs in a window, each of which is equivalent to a component placement area of the same size as an external container.

In this way, it is possible to put more components in one container (EG: Call history implementation mode).

Tabhost is just a simple container that provides the following two ways to create tabs, add tabs

= = "

Newtabspec (String tag): Create tab

AddTab (Tabhost.tabspec tabspec): Add tab

General steps for using Tabhost:

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

2.Activity inheritance tabactivity;

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

4. Create or add a tab by using the Tabhost object method;

Note: Tabhost also provides some ways to get the current tab, get the current view method, and through the Tabhost.ontabchangelistener listener-monitor changes to the current page sign Tabhost.

Tabwidget : The component is the top or bottom button in the Tabhost tab, and you can click the button to toggle the tab;

Tabspec : Represents the Tab interface, add a tabspec can be added to the tabhost;

Examples are as follows:

Layout file ==><tabhost xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "/http Schemas.android.com/tools "android:id=" @android: Id/tabhost "android:layout_width=" Match_parent "android:layout_he ight= "Match_parent" tools:context= ".        Main "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Match_parent " android:orientation= "vertical" > <tabwidget android:id= "@android: Id/tabs" Android:la Yout_width= "Match_parent" android:layout_height= "wrap_content" > </TabWidget> <framel Ayout android:id= "@android: Id/tabcontent" android:layout_width= "Match_parent" Android:lay out_height= "Wrap_content" > <linearlayout android:id= "@+id/tab1" Android:la Yout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "vertical " &Gt <textview android:layout_width= "match_parent" android:layout_height= "match_parent                "android:text="/> </LinearLayout> <linearlayout Android:id= "@+id/tab2" android:layout_width= "match_parent" android:layout_height= "Wrap_conte NT "android:orientation=" vertical "> <textview android:layout_width            = "Match_parent" android:layout_height= "match_parent" android:text= "/>" </LinearLayout> <linearlayout android:id= "@+id/tab3" a Ndroid:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:orientation= "Vertical" > <textview android:layout_width= "match_parent" Android Oid:layout_height= "match_parent" android:text= "/> </LinearLayout> </frame" Layout> </LinearLayout></TabHost> Code Implementation = = "Package Com.example.mytabhost;import Android.annotation.suppresslint;import Android.app.tabactivity;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.menu;import Android.widget.tabhost;import Android.widget.tabhost.ontabchangelistener;import Android.widget.tabhost.tabspec;import android.widget.Toast;@ Suppresslint ("Showtoast") @SuppressWarnings ("Deprecation") public class Mainactivity extends tabactivity{@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); final Tabhost tabhost = This.gettabhost (); Tabspec tab1 = Tabhost.newtabspec ("Tab1"). Setindicator ("Tab1"). SetContent (R.ID.TAB1); Tabhost.addtab (TAB1);// The above code is equivalent to Tabhost.addtab (Tabhost.newtabspec ("TaB2"). Setindicator ("TaB2"). SetContent (R.ID.TAB2)); tabhost. AddTab (Tabhost.newtabspec ("Tab3"). Setindicator ("Tab3"). SetContent (R.ID.TAB3)); Tabhost.setontabchangedlistener (New Ontabchangelistener () {@Overridepublic void ontabchanged (String tabId) {if (Tabid.equals ("Tab1")) {// The first label Toast.maketext (Mainactivity.this, "Selected 001", "X"). Show (); if (Tabid.equals ("TaB2")) {///second label Toast.maketext (Mainactivity.this, "002 selected", +). Show (); if (Tabid.equals ("Tab3")) {///Third label Toast.maketext (Mainactivity.this, "003 selected", +). Show ();}});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

Implementation results such as:

Android Learning Note 10--tabhost

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.