The tab is similar to the phone book interface. To achieve this effect, you must first know the tabhost, which is a container used to store multiple tab labels, each tab can correspond to its own layout. For example, the tab layout in the phone book is a linear layout that uses tabhost. First, you must use the gettabhost method to obtain the tabhost object, the addtab method is used to add a tab to tabhost. Of course, each tab generates an event during switchover. to capture this event, you need to set the event listening setontabchangedlistener for tabactivity. Below is a small example: tabtest. java view plaincopy to clipboardprint? Package Org. hualang. tab; import android. app. activity; import android. app. tabactivity; import android. graphics. color; import android. OS. bundle; import android. widget. tabhost; import android. widget. toast; import android. widget. tabhost. ontabchangelistener; public class tabtest extends tabactivity {/** called when the activity is first created. */tabhost; @ override public void oncreate (bundl E savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the tabhost object tabhost = gettabhost (); // Add a label for the tabhost // create a newtabspec (newtabspec) // set its label and Icon (setindicator) // set content (setcontent) tabhost. addtab (tabhost. newtabspec ("tab1 "). setindicator ("Tab 1", getresources (). getdrawable (R. drawable. img1 )). setcontent (R. id. text1); tabhost. addtab (tabhost. newtabspec ("tab2 "). setin Dicator ("Tab 2", getresources (). getdrawable (R. drawable. img2 )). setcontent (R. id. text2); tabhost. addtab (tabhost. newtabspec ("tab3 "). setindicator ("tab 3", getresources (). getdrawable (R. drawable. img3 )). setcontent (R. id. text3); // set the background color of tabhost // tabhost. setbackgroundcolor (color. argb (70,150, 22,); // sets the tabhost background image resource tabhost. setbackgroundresource (R. drawable. bg0); // set the label tabhost currently displayed. setcurrenttab (0 ); // Tag switching event processing, setontabchangedlistener tabhost. setontabchangedlistener (New ontabchangelistener () {public void ontabchanged (string Tabid) {Toast = toast. maketext (getapplicationcontext (), "now" + Tabid + "tag", toast. length_short); toast. show () ;}}) ;}} main. XML <? 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 = "fill_parent" Android: layout_height = "fill_parent"> <linearlayout Android: orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <tabwidget Android: Id = "@ Android: ID/tabs" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <framelayout Android: Id = "@ Android: ID/tabcontent" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: Id = "@ + ID/text1" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: TEXT = "Tab 1"/> <textview Android: Id = "@ + ID/text2" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: TEXT = "Tab 2"/> <textview Android: Id = "@ + ID/text3" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: TEXT = "tab 3"/> </framelayout> </linearlayout> </tabhost>
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.