Display Effect: change the background color of the tabhost label.
Modify idea: Listen to the ontabchanged method of tabhost.
Implementation Code:
Package com. Tony. tabstudy;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. tabhost;
Import Android. widget. tabhost. ontabchangelistener;
Import Android. widget. tabwidget;
Publicclass tabstudyactivity extends activity {
@ Override
Publicvoid oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Final tabhost = (tabhost) findviewbyid (R. Id. tabhost );
Tabhost. Setup ();
Tabhost. tabspec spec = tabhost. newtabspec ("tab1 ");
Spec. setcontent (R. Id. tab1 );
Spec. setindicator ("Homepage ");
Tabhost. addtab (SPEC );
Tabhost. tabspec spec2 = tabhost. newtabspec ("tab2 ");
Spec2.setcontent (R. Id. tab2 );
Spec2.setindicator ("homepage 2", getresources (). getdrawable (Android. R. drawable. btn_dialog ));
Tabhost. addtab (spec2 );
Tabhost. setcurrenttab (1 );
// Set the tag background once during initialization
Updatetabbackground (tabhost );
// The background changes when selected.
Tabhost. Setontabchangedlistener(New ontabchangelistener (){
@ Override
Publicvoid ontabchanged (string Tabid ){
Updatetabbackground (tabhost );
}
});
}
/**
* Update the tab background image.
* @ Param tabhost
*/
Privatevoid updatetabbackground (final tabhost ){
For (INT I = 0; I <tabhost. gettabwidget (). getchildcount (); I ++ ){
View vvv = tabhost. gettabwidget (). getchildat (I );
If (tabhost. getcurrenttab () = I ){
// The selected background
Vvv. setbackgrounddrawable (getresources (). getdrawable (Android. R. drawable. spinner_background ));
} Else {
// Unselected background
Vvv. setbackgrounddrawable (getresources (). getdrawable (R. drawable. ));
}
}
}
}