[Android] Use Activitygroup to toggle activity and layout

Source: Internet
Author: User

Objective

In a main interface to do activity switching is generally used tabactivity, easy to use, the activity is relatively independent of each other, but the customization is not strong, and it is cumbersome to modify. Of course, you can separate layout, the logic code is written in the main interface logic code, but obviously maintainability is quite poor, here through the activitygroup to solve the problem.

Statement

Welcome reprint, but please keep the original source of the article:)

Blog Park: http://www.cnblogs.com

Farmer Uncle: http://www.cnblogs.com/over140/

Article

1. Android:tabactivity Nested Activities

2. Android Activitygroup Use code to add sub-activty layout to the main activity

Body

One

You need to click on the different picture buttons at the bottom to toggle between different activity and display the activity's corresponding contentview in the middle.

Second, the implementation of the Code

2.1 Layout.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent" android:orientation= "vertical"Android:layout_height= "Fill_parent" > <linearlayout android:gravity= "Center_horizontal"Android:background= "@drawable/myinfor2" android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" > <textview android:id= "@+id/cust_title" android:textcolor= "@android: Color/white"android:textsize= "28SP" android:text= "Module 1" android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" ></TextView> </LinearLayout> <!--center Dynamically load View--<scrollview Android:measu Reallchildren= "true" android:id= "@+id/containerbody"Android:layout_weight= "1" android:layout_height= "Fill_parent"Android:layout_width= "Fill_parent" > </ScrollView> <linearlayout android:background= "@android: Color/black"android:layout_gravity= "Bottom" android:orientation= "horizontal"Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" > <!--Function Module Button 1--<imageview android:id= "@+i D/btnmodule1 "android:src=" @android:d Rawable/ic_dialog_dialer "Android:layout_marginleft= "7DP" android:layout_margintop= "3DP"Android:layout_marginbottom= "3DP" android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <!--function Module button 2--<imageview android:id= "@+id/btnmodule2" android:src= "@androi D:drawable/ic_dialog_info "Android:layout_marginleft= "7DP" android:layout_margintop= "3DP"Android:layout_marginbottom= "3DP" android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <!--Function Module button 3--<imageview android:id= "@+id/btnmodule3" android:src= "@androi D:drawable/ic_dialog_alert "Android:layout_marginleft= "7DP" android:layout_margintop= "3DP"Android:layout_marginbottom= "3DP" android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </LinearLayout></LinearLayout>

Testview.java

/*** Use Activitygroup to toggle activity and layout *@authorFarmer Uncle *@version2010-9-7 **/ Public classTestViewextendsActivitygroup {PrivateScrollView container =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Hide title barrequestwindowfeature (Window.feature_no_title); //setting up the ViewSetcontentview (r.layout.layout); Container=(ScrollView) Findviewbyid (r.id.containerbody); //Module 1ImageView BtnModule1 =(ImageView) Findviewbyid (R.ID.BTNMODULE1); Btnmodule1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {container.removeallviews (); Container.addview (Getlocalactivitymanager (). StartActivity ("Module1",                        NewIntent (TestView. This, ModuleView1.class). Addflags (Intent.flag_activity_clear_top)). Getdecorview ());        }        }); //Module 2ImageView BtnModule2 =(ImageView) Findviewbyid (r.id.btnmodule2); Btnmodule2.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {container.removeallviews (); Container.addview (Getlocalactivitymanager (). StartActivity ("Module2",                        NewIntent (TestView. This, ModuleView2.class). Addflags (Intent.flag_activity_clear_top)). Getdecorview ());        }        }); //Module 3ImageView BtnModule3 =(ImageView) Findviewbyid (R.ID.BTNMODULE3); Btnmodule3.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {container.removeallviews (); Container.addview (Getlocalactivitymanager (). StartActivity ("Module3",                        NewIntent (TestView. This, MODULEVIEW3.class). Addflags (Intent.flag_activity_clear_top)). Getdecorview ());    }        }); }}

Code Description:

a). ModuleView1, ModuleView2 and MODULEVIEW3 respectively inherit from activity.

b). If you want to change the title dynamically, you can set it by Cust_title get TextView.

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: info-contact@alibabacloud.com 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.