Android Learning--tabhost Tab component

Source: Internet
Author: User

Tabhost is a very useful component, and tabhost can easily place multiple tabs on a window, each of which has a component placement area of the same size as an external container. The application of mobile phone system is similar to "Missed Call", "answered phone", "outgoing call" and so on.

1. Tabhost provides two ways to create a tab, add a tab

Newtabspec (String tag): Create tab

AddTab (Tabhost.tabspec tabspec): Add tab


2, tabhost Toggle tab triggered by the monitoring is Tabhost.ontabchangelistener



Follow the example below to familiarize yourself with Tabhost

The general steps for using Tabhost are:

(1) Define the contents of the re-entry card for Tabhost in the interface layout

(2) Activity succession tabactivity

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

(4) Create tabs, add tabs by Tabhost object methods


Follow the steps above to implement the Tabhost case


(1) In the interface layout for Tabhost definition of the contents of the re-entry card, the general use of Framelayout as the root layout, each label page corresponding to a child node layout

<!--This is the root layout--<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:layout_widt             H= "Fill_parent" android:layout_height= "Fill_parent" > <!--This is the first tab layout--<linearlayout Android:id= "@+id/widget_layout_blue" android:layout_width= "Fill_parent" Android:layout_hei ght= "fill_parent" android:orientation= "vertical" > <edittext android:id= "@+id/                 Widget34 "android:layout_width=" fill_parent "android:layout_height=" Wrap_content " android:text= "EditText" android:textsize= "18SP" > </EditText> <butto n android:id= "@+id/widget30" android:layout_width= "Wrap_content" Android:lay out_height= "wrap_content" android:text= "button" > </Button> </linearlayout&gt                 ; <!--This is the second tab layout--<linearlayout android:id= "@+id/widget_layout_red" Android:layout_widt            H= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <analogclock android:id= "@+id/widget36" android:layout_width= "Wrap_content" android:layou t_height= "Wrap_content" > </AnalogClock> </LinearLayout> <!--This is the third tab cloth             ---<linearlayout android:id= "@+id/widget_layout_green" android:layout_width= "Fill_parent"                android:layout_height= "fill_parent" android:orientation= "vertical" > <radiogroup Android:id= "@+id/widget43" android:layout_width= "166px" android:layout_height= "98px                    "android:orientation=" vertical "> <radiobutton android:id=" @+id/widget44 " Android:layout_wiDth= "Wrap_content" android:layout_height= "wrap_content" android:text= "male" > </RadioButton> <radiobutton android:id= "@+id/widget45" Android:layout_                Width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Women" > </RadioButton> </RadioGroup> </LinearLayout> </FrameLayout>

(2) Activity succession tabactivity

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

(4) Create tabs, add tabs by Tabhost object methods


Package Com.example.tabhost;import Android.app.tabactivity;import Android.os.bundle;import android.util.Log;import Android.view.layoutinflater;import Android.widget.tabhost;import android.widget.tabhost.ontabchangelistener;@ Suppresswarnings ("deprecation") public class Mainactivity extends tabactivity{@Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate);//Get Tabhost object Tabhost tabhost = Gettabhost ();// Set use Tabhost layout//from (this) to get layoutinflater//r.layout.main from this tabactivity to store tab layout// Whether the framelayout//that holds the tab page content is inflate tied to the root layout element Layoutinflater.from (this) by Tabhost. Inflate (R.layout.activity_main , Tabhost.gettabcontentview (), true);//Add the first tab//setindicator add a table body, which can be view//add tab content Layout Tabhost.addtab ( Tabhost.newtabspec ("Tab1"). Setindicator ("TAB1"). SetContent (R.id.widget_layout_blue)); Add a second tab tabhost.addtab (Tabhost.newtabspec ("tab2")///Place an icon on the label header. Setindicator ("TAB2"). SetContent (r.id.widget_ layout_red)); Add a third tab Tabhost.addtab (Tabhost.newtabspec ("Tab3"). SetindIcator ("TAB3"). SetContent (R.id.widget_layout_green));   Add Listener Tabhost.setontabchangedlistener (new Ontabchangelistener () {@Overridepublic void ontabchanged (String tabId) { LOG.I ("tab", TabId);}});}}

After running, the effect is 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.