The implementation of Android tabwidget can be divided into two types: Standard tabactivity implementation and custom implementation. This method is relatively complicated to implement, however, it is good to implement a more diversified view. Here we will look at the source code.
I. general practices
Inherit tabactivity and implement your own tabactivity
Import android. app. activity; <br/> Import android. app. tabactivity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. widget. tabhost; <br/> Import android. widget. tabhost. ontabchangelistener; <br/> public class tabwidgetdemo2 extends tabactivity implements ontabchangelistener {<br/> private tabhost mtabhost; </P> <p> @ override <br/> protected void oncreate (bundle savedi Nstancestate) {<br/> // todo auto-generated method stub <br/> super. oncreate (savedinstancestate); </P> <p> setcontentview (R. layout. tabwidgetdemo2); <br/> mtabhost = gettabhost (); <br/> mtabhost. setontabchangedlistener (this); <br/> setuptab1 (); <br/> setuptab2 (); <br/> mtabhost. setcurrenttab (1); <br/>}< br/> private void setuptab2 () {<br/> // todo auto-generated method stub <br/> intent = new inte NT (); <br/> intent. setaction (intent. action_main); <br/> intent. setclass (this, tabwidget2.class); <br/> mtabhost. addtab (mtabhost. newtabspec ("tabwidget2") <br/>. setindicator ("tabwidget2", getresources (). getdrawable (R. drawable. icon) <br/>. setcontent (intent); <br/>}< br/> private void setuptab1 () {<br/> // todo auto-generated method stub <br/> intent = new intent (); <br/> intent. setaction (intent. Action_main); <br/> intent. setclass (this, tabwidget1.class); <br/> mtabhost. addtab (mtabhost. newtabspec ("tabwidget1") <br/>. setindicator ("tabwidget1", getresources (). getdrawable (R. drawable. icon) <br/>. setcontent (intent); <br/>}< br/> Public void ontabchanged (string Tabid) {<br/> // todo auto-generated method stub <br/> activity = getlocalactivitymanager (). getactivity (Tabid); <br/> If (AC Tietong! = NULL) {<br/> activity. onwindowfocuschanged (true); <br/>}</P> <p>}
For the activity corresponding to the two tabs, see tabwidget1 first. This class will be used in the second implementation, so we can see the judgment on the action.
Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import COM. android. exampledemo. r; <br/> Import COM. android. exampledemo. util. demoutils; <br/> public class tabwidget1 extends activity {<br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> // todo auto-generated method stub <br/> super. oncreate (savedinstancestate); </P> <p> intent = This. getintent (); <br/> If (intent. getaction (). equals (intent. action_main) {<br/> setcontentview (R. layout. tabwidgetdemo2_1); <br/>}< br/> else {<br/> setcontentview (R. layout. tabwidget_1); <br/> demoutils. updatebuttonbar (activity) This, R. id. contactstab); <br/>}< br/>
Let's take a look at tabwidget2. We will also use this activity in the second implementation method.
Import COM. android. exampledemo. r; <br/> Import COM. android. exampledemo. util. demoutils; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> public class tabwidget2 extends activity {<br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> // todo auto-generated method stub <br/> super. oncreate (savedinstancestate); </P> <p> intent = This. getintent (); </P> <p> If (intent. getaction (). equals (intent. action_main) {<br/> setcontentview (R. layout. tabwidgetdemo2_1); <br/>}< br/> else {<br/> setcontentview (R. layout. tabwidget_2); <br/> demoutils. updatebuttonbar (activity) This, R. id. groupstab); <br/>}< br/>}
Layout corresponding to each activity
1. tabwidgetdemo2.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <tabhost <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Id = "@ Android: ID/tabhost" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <linearlayout <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <tabwidget Android: Id = "@ Android: ID/tabs "<br/> Android: layout_width =" fill_parent "<br/> Android: layout_height =" 68dip "<br/> Android: paddingleft = "1dip" <br/> Android: paddingright = "1dip" <br/> Android: paddingtop = "4dip" <br/> <framelayout Android: Id = "@ Android: ID/tabcontent" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "0dip" <br/> Android: layout_weight = "1" <br/> </linearlayout> <br/> </tabhost>
2. layout corresponding to the two Sub tabs
Layout1 <br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Background = "# fff"> <br/> <textview Android: id = "@ + ID/textview" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "tab widget first"> <br/> </textview> <br/> </linearlayou T> <br/> layout2 <br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Background = "# fff"> <br/> <textview Android: id = "@ + ID/textview" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "tab widget second"> <br/> </textview> <br/> </linearlayout>
Method 2:
Create an activity first (tabwidgetdemo)
1. tabwidgetdemo. java <br/> Import COM. android. exampledemo. r; <br/> Import COM. android. exampledemo. util. demoutils; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. content. sharedpreferences; <br/> Import android. OS. bundle; <br/> // not use tabhost to organized <br/> public class tabwidgetdemo extends activity {<br/> @ override <br/> protected void oncreate (bundle SA Vedinstancestate) {<br/> // todo auto-generated method stub <br/> super. oncreate (savedinstancestate); <br/> // int activetab = demoutils. getintpref (this, "activetab", R. id. artisttab); <br/> sharedpreferences prefs = <br/> getsharedpreferences (getpackagename (), context. mode_private); <br/> int activetab = prefs. getint ("activetab", R. id. contactstab); <br/> If (activetab! = R. Id. contactstab <br/> & amp; activetab! = R. id. groupstab) {<br/> activetab = R. id. contactstab; <br/>}< br/> demoutils. activatetab (this, activetab); <br/>}< br/> 2. demoutils <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android.net. uri; <br/> Import android. view. view; <br/> Import android. widget. tabwidget; <br/> Import COM. android. exampledemo. r; <br/> public class demoutils {<br/> static int sactivetabindex =-1; </P> <p> Public static void activatetab (Activity A, int active_id) {<br/> intent = new intent (intent. action_pick); <br/> switch (active_id) {<br/> case R. id. contactstab: <br/> intent. setdataandtype (URI. empty, "Vnd. android. cursor. DIR/tb_contacts "); <br/> break; <br/> case R. id. groupstab: <br/> intent. setdataandtype (URI. empty, "Vnd. android. cursor. DIR/tb_groups "); <br/> break; <br/> default: <br/> return; <br/>}< br/> intent. addflags (intent. flag_activity_clear_top); <br/>. startactivity (intent); <br/>. finish (); <br/>. overridependingtransition (0, 0); <br/>}</P> <p> Public static void updatebuttonbar (Activity A, int highlight) {<br/> final tabwidget LL = (tabwidget) a. findviewbyid (R. id. buttonbar); </P> <p> for (INT I = ll. getchildcount ()-1; I> = 0; I --) {<br/> View v = ll. getchildat (I); <br/> Boolean isactive = (V. GETID () = highlight); <br/> If (isactive) {<br/> ll. setcurrenttab (I); <br/> sactivetabindex = I; <br/>}</P> <p> v. settag (I); <br/> v. setonclicklistener (new view. onclicklistener () {<br/> Public void onclick (view v) {<br/> int id = v. GETID (); <br/> If (ID = ll. getchildat (sactivetabindex ). GETID () {<br/> return; <br/>}< br/> activatetab (activity) ll. getcontext (), ID); <br/> ll. setcurrenttab (integer) v. gettag (); <br/>}}); <br/>}< br/>}
The first method of the two tab sub activities has been provided. Here we only need to look at the implementation of layout.
1> buttonbar. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <tabwidget xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Id = "@ + ID/buttonbar" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "wrap_content"> <br/> <textview <br/> Android: id = "@ + ID/contactstab" <br/> Android: focusable = "true" <br/> Android: drawabletop = "@ drawable/icon" <br/> Android: background = "@ drawable/buttonbarbackground" <br/> Android: text = "C Ontacts "<br/> Android: textcolor =" @ color/tab_indicator_text "<br/> Android: textappearance = "? Android: ATTR/textappearancesmall "<br/> Android: paddingtop =" 7dip "<br/> Android: paddingbottom =" 2dip "<br/> Android: gravity = "center" <br/> Android: layout_weight = "1" <br/> Android: layout_marginleft = "-3dip" <br/> Android: layout_marginright = "-3dip" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "84dip" <br/> Android: singleline = "true" <br/> Android: ellipsize = "marquee"/> <br/> <te Xtview <br/> Android: Id = "@ + ID/groupstab" <br/> Android: focusable = "true" <br/> Android: drawabletop = "@ drawable/icon" <br/> Android: Background = "@ drawable/buttonbarbackground" <br/> Android: text = "group" <br/> Android: textcolor = "@ color/tab_indicator_text" <br/> Android: textappearance = "? Android: ATTR/textappearancesmall "<br/> Android: paddingtop =" 7dip "<br/> Android: paddingbottom =" 2dip "<br/> Android: gravity = "center" <br/> Android: layout_weight = "1" <br/> Android: layout_marginleft = "-3dip" <br/> Android: layout_marginright = "-3dip" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "84dip" <br/> Android: singleline = "true" <br/> Android: ellipsize = "marquee"/> <br/> </tabwidget> <br/>
2> tabwidget_1.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <include layout = "@ layout/battonbar"/> </P> <p> <expandablelistview Android: id = "@ + ID/Android: List" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: footerdividersenabled = "true" <br/> Android: fadescrollbars = "true" <br/> Android: drawselectofftop = "true"> <br/> </expandablelistview> </P> <p> </linearlayout>
3> tabwidget_2.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <include layout = "@ layout/battonbar"/> </P> <p> </linearlayout>
I will not provide some other resource files. tabwidget can be implemented in the above two methods. The first method is more common, the second method can be used to customize some styles, and some complicated views can be made, hope to help you!