The android. app package contains an activitygroup class, which is the activity container and can contain multiple nested activitys. This article uses the activitygroup function to nest the activity to implement the tab function. The tab UI can be seen in many mobile applications, including Android, iPhone, window phone7 and other mobile terminals. The tab UI Mode features a large size of a small view.
First, we can know from the SDK documentation that the parent class of the activitygroup class is activity (SEE), that is, the two have the same interface and lifecycle, the same as the activity, there are also functions such as oncreate () and onpause () for reload.
Activitygroup has two public methods (): activitygroup can call the getlocalactivitymanage () method to obtain localactitymanage to manage the activity.
Activitygroup provides the following tabs.
Let's take a look at the Java code:
Public class mainview extends activitygroup {@ suppresswarnings ("UNUSED") Private linearlayout bodyview, headview; private linearlayout One, Two, Three, Four; private int flag = 0; // jump to different pages by marking and display different menu items/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. view_main); initmainview (); // showview (FLAG); one. setonclicklistener (New onclicklistener () {public void onclick (view v) {// todo auto-generated method stubflag = 0; showview (FLAG) ;}}); Two. setonclicklistener (New onclicklistener () {public void onclick (view v) {// todo auto-generated method stubflag = 1; showview (FLAG) ;}}); three. setonclicklistener (New onclicklistener () {public void onclick (view v) {// todo auto-generated method stubflag = 2; showview (FLAG) ;}}); four. setonclicklistener (New onclicklistener () {public void onclick (view v) {// todo auto-generated method stubflag = 3; showview (FLAG );}});} /** initialize the main interface */Public void initmainview () {headview = (linearlayout) findviewbyid (R. id. head); bodyview = (linearlayout) findviewbyid (R. id. body); one = (linearlayout) findviewbyid (R. id. one); two = (linearlayout) findviewbyid (R. id. two); three = (linearlayout) findviewbyid (R. id. three); four = (linearlayout) findviewbyid (R. id. four);} // display other public void showview (INT flag) {Switch (FLAG) {Case 0: bodyview on the main interface. removeallviews (); view v = getlocalactivitymanager (). startactivity ("one", new intent (mainview. this, oneview. class )). getdecorview (); one. setbackgroundresource (R. drawable. frame_button_background); Two. setbackgroundresource (R. drawable. frame_button_nopressbg); three. setbackgroundresource (R. drawable. frame_button_nopressbg); four. setbackgroundresource (R. drawable. frame_button_nopressbg); bodyview. addview (V); break; Case 1: bodyview. removeallviews (); bodyview. addview (getlocalactivitymanager (). startactivity ("two", new intent (mainview. this, twoview. class )). getdecorview (); one. setbackgroundresource (R. drawable. frame_button_nopressbg); Two. setbackgroundresource (R. drawable. frame_button_background); three. setbackgroundresource (R. drawable. frame_button_nopressbg); four. setbackgroundresource (R. drawable. frame_button_nopressbg); break; Case 2: bodyview. removeallviews (); bodyview. addview (getlocalactivitymanager (). startactivity ("three", new intent (mainview. this, threeview. class )). getdecorview (); one. setbackgroundresource (R. drawable. frame_button_nopressbg); Two. setbackgroundresource (R. drawable. frame_button_nopressbg); three. setbackgroundresource (R. drawable. frame_button_background); four. setbackgroundresource (R. drawable. frame_button_nopressbg); break; Case 3: bodyview. removeallviews (); bodyview. addview (getlocalactivitymanager (). startactivity ("four", new intent (mainview. this, fourview. class )). getdecorview (); one. setbackgroundresource (R. drawable. frame_button_nopressbg); Two. setbackgroundresource (R. drawable. frame_button_nopressbg); three. setbackgroundresource (R. drawable. frame_button_nopressbg); four. setbackgroundresource (R. drawable. frame_button_background); break; default: Break ;}}}
What is important in the program is the following method:
bodyView.removeAllViews(); bodyView.addView(getLocalActivityManager().startActivity("two",new Intent(MainView.this, TwoView.class)).getDecorView());
Use the removeallviews () method of the view to clear unnecessary views, and use the addview (view v) method to add the required views.
Getlocalactivitymanager (). startactivity ("two", new intent (mainview. This, twoview. Class) to get a window object, the window object is called
Getdecorview () to get the view. For more information about the window method, see Android. App. Window.
You can see through the tab that this effect uses three layout. Implement layout to implement the tab function.