Android Custom tabactivity (Implementation of Sina Weibo bottom menu update UI)

Source: Internet
Author: User

Many apps on Android today use the framework of the bottom menu to control the updated UI, such as Sina Weibo

Click on the options at the bottom menu to update the interface. The bottom menu can be implemented using Tabhost, but people who have used Tabhost know that custom tabhost is a lot of trouble, the original tabhost style is not attached to the bottom of the screen, to attach the bottom of the layout should be rewritten.

Tabhost settings container can manage the display of the UI, the UI can be dynamically generated with layoutinflater, or it can be activity, but it's not good to manage the activity's life cycle. The display of the UI is then controlled with Tabhost.

One way to do this is to customize the menu layout +activitygroup+ multiple activity implementations, the following is demo:


Activitygroup, as the name implies, is the activity group, which can manage the startup and destruction of multiple activity. Activitygroup is a successor to the activity, but this method has now been deprecated, although not recommended, but it can be used. The recommended practice will be spoken later. We will use this class to manage the implementation of the interface. An important method in Activitygroup is Getlocalactivitymanager, which destroys and launches new activity, and can be displayed through the Getdecorview method to get to the root view of the startup activity. Activity is displayed in a container in activitygroup, and container is an area of activity that must be viewgroup or its subclasses.

First write a tabactivity

Package Com.shamoo.activity;import;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.view.viewgroup;import Android.view.Window;public class Tabactivity extends Activitygroup {private viewgroup container; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Requestwindowfeature ( Window.feature_no_title);} /** * Set activity display by ID container, the container must be inherited viewgroup */protected void Setcontainer (int resId) {container = ( ViewGroup) Findviewbyid (resId);} /** * Show activity by Class activity */protected void showactivity (class<?> activityclass) {Intent Intent = new Intent ( this, activityclass);//Check if container has the activity shown, and if so, first remove view activity = Container.getchildat (0); if (activity! = NULL) {//Remove the displayed activity's viewcontainer.removeallviews ();//Remove Activitygetlocalactivitymanager () by Activitymanager. Removeallactivities ();} Starts a new activity and adds the activity's root view to the COntanier in Container.addview (Getlocalactivitymanager (). StartActivity (Activityclass.getname (), intent). Getdecorview ());}}

Write a mainactivity management interface that inherits Tabactivity, which is a three activity

Package Com.shamoo.activity;import Com.shamoo.activitygroupdemo.r;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class MainActivity Extends Tabactivity implements Onclicklistener {/** * shows three activity Class */private class<?> activities[] = {Oneact Ivity.class, Twoactivity.class, threeactivity.class};/** * Menu three buttons */private button[] btn = new button[3];/** * Current selection */PR ivate int currentselect; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsetcontentview (R.layout.activity_main); Setcontainer (; btn[0] = (Button) Findviewbyid (; Btn[0].setonclicklistener (this); btn[1] = (Button) Findviewbyid (; btn[1]. Setonclicklistener (this), btn[2] = (Button) Findviewbyid (; Btn[2].setonclicklistener (this); Showactivity (Activities[0]);} @Overridepublic void OnClick (View v) {//TODO auto-generated method stubbtn[currentselect].seTbackgroundresource (r.color.normal); switch (V.getid ()) {case = 0;btn[0]. Setbackgroundresource (; Showactivity (Activities[0]); Break;case = 1;btn[1 ].setbackgroundresource (; Showactivity (activities[1]); Break;case = 2; Btn[2].setbackgroundresource (; Showactivity (activities[2]);

Write Activity_main.xml, which has the implementation of the bottom menu, which is implemented by a button that modifies the background by LinearLayout Layout_weight mates.

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "        Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <linearlayout Android:id= "@+id/rl_menu" android:layout_width= "match_parent" android:layout_height= "45DP" Android: Layout_alignparentbottom= "true" android:orientation= "horizontal" > <button android:id= "@+id /btn_one "android:layout_width=" match_parent "android:layout_height=" Match_parent "Androi            d:layout_weight= "1" android:background= "#008eff" android:text= "1"/> <button            Android:id= "@+id/btn_two" android:layout_width= "match_parent" android:layout_height= "Match_parent" android:layout_weight= "1" android:background= "#555555" android:text= "2"/> < Button Android:id= "@+iD/btn_three "android:layout_width=" match_parent "android:layout_height=" Match_parent "and    roid:layout_weight= "1" android:background= "#555555" android:text= "3"/> </LinearLayout> <framelayout android:id= "@+id/fl_container" android:layout_width= "Match_parent" Android:layout_ height= "Match_parent" android:layout_above= "@id/rl_menu" > </framelayout></relativelayout&gt ;

Then write three activity, and these three activity can be customized. The code is more, it is not all posted out.

After launching the demo, you can see that the activity Lifecycle management is no problem.

Demo Download Link:

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: 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.