Full Set of Android tabactivity code (implemented in combination with the layout XML file)

Source: Internet
Author: User

Today, I checked the usage of tabactivity. The articles on the Internet are not comprehensive and are not detailed enough. Therefore, the entire code is specially organized for your convenience. I wrote the source code for Android for the first time. If you do not have any deficiencies, please kindly advise me ~ If you have less time to talk about it, go to the topic below.

 

My development environment is eclipse + Android sdk1.6.

 

Manifest. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. keven "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". mainframe "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "4"/> </P> <p> </manifest>

 

Mainframe. Java:

Package COM. keven; </P> <p> Import android. app. alertdialog; <br/> Import android. app. tabactivity; <br/> Import android. OS. bundle; <br/> Import android. view. layoutinflater; <br/> Import android. view. menu; <br/> Import android. view. menuinflater; <br/> Import android. widget. tabhost; <br/> Import android. widget. tabhost. ontabchangelistener; <br/> Import android. widget. tabhost. tabspec; </P> <p> public class mainframe extends tabactivity implements ontabchangelistener {<br/> private tabhost; <br/> private tabspec tab1, tab2, tab3; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> tabhost = This. gettabhost (); <br/> layoutinflater. from (this ). inflate (R. layout. main, <br/> tabhost. gettabcontentview (), true); <br/> tab1 = tabhost. newtabspec ("id1 "). setindicator ("tab1", <br/> This. getresources (). getdrawable (R. drawable. icon )). setcontent (<br/> r. id. tab1); <br/> tabhost. addtab (tab1); <br/> tab2 = tabhost. newtabspec ("ID2 "). setindicator ("tab2", <br/> This. getresources (). getdrawable (R. drawable. icon )). setcontent (<br/> r. id. tab2); <br/> tabhost. addtab (tab2); <br/> tab3 = tabhost. newtabspec ("ID3 "). setindicator ("tab3", <br/> This. getresources (). getdrawable (R. drawable. icon )). setcontent (<br/> r. id. tab3); <br/> tabhost. addtab (tab3); <br/> tabhost. setontabchangedlistener (this); <br/>}</P> <p> @ override <br/> Public void ontabchanged (string Tabid) {<br/> New alertdiener. builder (this ). settitle ("hint "). setmessage (Tabid) <br/>. setpositivebutton ("OK", null ). show (); <br/>}</P> <p> private int [] tabmenus = {R. menu. menu1, R. menu. menu2, R. menu. menu3 }; </P> <p> Public Boolean onprepareoptionsmenu (menu) {<br/> menu. clear (); <br/> menuinflater Inflater = getmenuinflater (); <br/> switch (tabhost. getcurrenttab () {<br/> case 0: <br/> Inflater. inflate (tabmenus [0], menu); <br/> break; <br/> case 1: <br/> Inflater. inflate (tabmenus [1], menu); <br/> break; <br/> case 2: <br/> Inflater. inflate (tabmenus [2], menu); <br/> break; <br/> default: <br/> break; <br/>}</P> <p> return Super. oncreateoptionsmenu (menu); <br/>}</P> <p>}

 

Layout file, Main. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <framelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <linearlayout Android: Id = "@ + ID/tab1" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" androidrientation = "vertical"> <br/> </linearlayout> </P> <p> <linearlayout Android: id = "@ + ID/tab2" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" androidrientation = "vertical"> <br/> </linearlayout> </P> <p> <linearlayout Android: id = "@ + ID/tab3" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" androidrientation = "vertical"> <br/> </linearlayout> </P> <p> </framelayout>

 

Menu file, menu1.xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: Id = "@ + ID/item01" Android: title = "tab1 M1"> </item> <br/> <item Android: Id = "@ + ID/item02" Android: title = "tab1 m2"> </item> <br/> <item Android: Id = "@ + ID/item03" Android: title = "tab1 m3"> </item> <br/> </menu> <br/>

Menu file, menu2.xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: Id = "@ + ID/item04" Android: title = "tab2 M1"> </item> <br/> <item Android: Id = "@ + ID/item05" Android: title = "tab2 m2"> </item> <br/> <item Android: Id = "@ + ID/item06" Android: title = "tab2 m3"> </item> <br/> </menu> <br/>

Menu file, menu3.xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <menu <br/> xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: Id = "@ + ID/item07" Android: title = "tab3 M1"> </item> <br/> <item Android: Id = "@ + ID/item08" Android: title = "tab3 m2"> </item> <br/> <item Android: Id = "@ + ID/item09" Android: title = "tab3 m3"> </item> <br/> </menu> <br/>

 

 

Related Article

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.