From http://www.eoeandroid.com/thread-1035-1-1.html
Tab and tabhost
This is the tab, and the container that holds the tab is the tabhost.
How to implement it ??
Each tab also corresponds to a layout, which is a bit fun. An activity corresponds to Multiple Functional la S.
① Create a tab project. do not generate the main activity.
Do not select
② Create a class mytab in the package and inherit from tabactivity.
In fact, tabactivity is a subclass of activity.
- Package ZYF. Tab. test;
- Import Android. App. tabactivity;
- Public class mytab extends tabactivity {
- }
Copy code
③ Inherit the oncreate () entry method from the parent class
- Package ZYF. Tab. test;
- Import Android. App. tabactivity;
- Import Android. OS. Bundle;
- Public class mytab extends tabactivity {
- @ Override
- Protected void oncreate (bundle savedinstancestate ){
- // Todo auto-generated method stub
- Super. oncreate (savedinstancestate );
- }
- }
Copy code
④ Register the mytab class (activity) in the manifest. xml file)
- <Activity Android: Name = ". mytab">
- <Intent-filter>
- <Action Android: Name = "android. Intent. Action. Main"> </Action>
- <Category Android: Name = "android. Intent. Category. launcher"> </Category>
- </Intent-filter>
- </Activity>
Copy code
⑤ At this time, you need to design the layout corresponding to the tab. Generally, framelayout is used as the root layout. Each tab page corresponds to the layout of a subnode.
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! -- Root node layout -->
- <Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "fill_parent" Android: layout_height = "fill_parent">
- <! -- Layout of the first tab -->
- <Linearlayout Android: Id = "@ + ID/widget_layout_blue"
- Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
- Androidrientation = "vertical">
- <Edittext Android: Id = "@ + ID/widget34" Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content" Android: text = "edittext"
- Android: textsize = "18sp">
- </Edittext>
- <Button Android: Id = "@ + ID/widget30" Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content" Android: text = "button">
- </Button>
- </Linearlayout>
- <! -- Layout of the second tab -->
- <Linearlayout Android: Id = "@ + ID/widget_layout_red"
- Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
- Androidrientation = "vertical">
- <Analogclock Android: Id = "@ + ID/widget36"
- Android: layout_width = "wrap_content" Android: layout_height = "wrap_content">
- </Analogclock>
- </Linearlayout>
- <! -- Layout of the third tab -->
- <Linearlayout Android: Id = "@ + ID/widget_layout_green"
- Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
- Androidrientation = "vertical">
- <Radiogroup Android: Id = "@ + ID/widget43"
- Android: layout_width = "166px" Android: layout_height = "98px"
- Androidrientation = "vertical">
- <Radiobutton Android: Id = "@ + ID/widget44"
- Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"
- Android: text = "radiobutton">
- </Radiobutton>
- <Radiobutton Android: Id = "@ + ID/widget45"
- Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"
- Android: text = "radiobutton">
- </Radiobutton>
- </Radiogroup>
- </Linearlayout>
- </Framelayout>
Copy code
⑥ First, we should declare the tabhost, and then use layoutinflater to filter out the layout and add the framelayout containing the tab page to the tabhost.
- Private tabhost mytabhost;
- Mytabhost = This. gettabhost (); // obtain the tabhost on the tabactivity
- Layoutinflater. From (this). Inflate (R. layout. Main, mytabhost. gettabcontentview (), true );
- // From (this) Get layoutinflater from this tabactivity
- // R. layout. Main stores the tab Layout
- // Use tabhost to obtain the framelayout that stores the tab content.
- // Whether to tie inflate to the root layout element
- Mytabhost. setbackgroundcolor (color. argb (150, 22, 70,150 ));
- // Set the color of tabhost.
Copy code
7. Create a label in tabhost and set the title/icon/TAB layout.
- Mytabhost
- . Addtab (mytabhost. newtabspec ("TT") // create a new tag TT
- . Setindicator ("KK ",
- Getresources (). getdrawable (R. drawable. ajjc ))
- // Set the title to KK and the label icon to ajjc.
- . Setcontent (R. Id. widget_layout_red ));
- // Set the layout of this tab to R. Id. widget_layout_red. This is a sub-layout in framelayout.
Copy code
Handler tag switching event processing, setontabchangedlistener
- Mytabhost. setontabchangedlistener (New ontabchangelistener (){
- @ Override
- Public void ontabchanged (string Tabid ){
- // Todo auto-generated method stub
- }
- });
Copy code
Dynamic menu of each tab
Put the menu designed in XML into an int array.
- Private Static final int mymenuresources [] = {R. Menu. phonebook_menu,
- R. Menu. addphone_menu, R. Menu. chatting_menu, R. Menu. userapp_menu };
Copy code
In the setontabchangedlistener () method, set the mymenusettingtag Based on the tag switch.
- @ Override
- Public void ontabchanged (string tagstring ){
- // Todo auto-generated method stub
- If (tagstring. Equals ("one ")){
- Mymenusettingtag = 1;
- }
- If (tagstring. Equals ("two ")){
- Mymenusettingtag = 2;
- }
- If (tagstring. Equals ("three ")){
- Mymenusettingtag = 3;
- }
- If (tagstring. Equals ("four ")){
- Mymenusettingtag = 4;
- }
- If (mymenu! = NULL ){
- Oncreateoptionsmenu (mymenu );
- }
- }
Copy code
Then, the oncreateoptionsmenu (menu) method dynamically adds the menu through the menuinflater Filter
- @ Override
- Public Boolean oncreateoptionsmenu (menu ){
- // Todo auto-generated method stub
- // Hold on to this
- Mymenu = menu;
- Mymenu. Clear (); // clear the menu
- // Inflate the currently selected menu XML resource.
- Menuinflater Inflater = getmenuinflater ();
- // Obtain a menu filter from tabactivity
- Switch (mymenusettingtag ){
- Case 1:
- Inflater. Inflate (mymenuresources [0], menu );
- // Dynamically Add the corresponding XML menu in the array
- Break;
- Case 2:
- Inflater. Inflate (mymenuresources [1], menu );
- Break;
- Case 3:
- Inflater. Inflate (mymenuresources [2], menu );
- Break;
- Case 4:
- Inflater. Inflate (mymenuresources [3], menu );
- Break;
- Default:
- Break;
- }
- Return super. oncreateoptionsmenu (menu );
- }
Copy code
Worker Running Effect