[Android] Use ActivityGroup to switch between Activity and Layout

Source: Internet
Author: User

Preface

In a main interface, TabActivity is usually used for Activity switching, which is convenient to use. The Activity is relatively independent from each other, but the customization is not strong, and it is difficult to modify. Of course, you can also separate layout and write all the logic code in the logic code on the main interface, but it is obvious that the maintainability is quite poor. ActivityGroup is used to solve this problem.

 

 

Statement

You are welcome to repost, but please keep the original source of the article :)

Blog: http://www.cnblogs.com

Farmer's uncle: http://www.cnblogs.com/over140/

 

 

Article

1. Android: TabActivity Nested Activities

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

 

Body

I,

    

You must click the different image buttons at the bottom to switch between different activities and display the ContentView of the Activity in the middle.

 

II. Implementation 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>
<! -- Dynamically load View in the middle -->
<ScrollView android: measureAllChildren = "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 = "@ + id/btnModule1" android: src = "@ android: drawable/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 = "@ android: 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 = "@ android: 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>

2.2 TestView. java

/**
* Use ActivityGroup to switch between Activity and Layout.
* @ Author: farmer's uncle
* @ Version 2010-9-7
*
*/
Public class TestView extends ActivityGroup {

Private ScrollView container = null;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Hide the title bar
RequestWindowFeature (Window. FEATURE_NO_TITLE );
// Set the view
SetContentView (R. layout. layout );

Container = (ScrollView) findViewById (R. id. containerBody );

// Module 1
ImageView btnModule1 = (ImageView) findViewById (R. id. btnModule1 );
BtnModule1.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Container. removeAllViews ();
Container. addView (getLocalActivityManager (). startActivity (
"Module1 ",
New Intent (TestView. this, ModuleView1.class)
. AddFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP ))
. GetDecorView ());
}
});

// Module 2
ImageView btnModule2 = (ImageView) findViewById (R. id. btnModule2 );
BtnModule2.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Container. removeAllViews ();
Container. addView (getLocalActivityManager (). startActivity (
"Module2 ",
New Intent (TestView. this, ModuleView2.class)
. AddFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP ))
. GetDecorView ());
}
});

// Module 3
ImageView btnModule3 = (ImageView) findViewById (R. id. btnModule3 );
BtnModule3.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Container. removeAllViews ();
Container. addView (getLocalActivityManager (). startActivity (
"Module3 ",
New Intent (TestView. this, ModuleView3.class)
. AddFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP ))
. GetDecorView ());
}
});
}
}

Code Description:

A). ModuleView1, ModuleView2, and ModuleView3 inherit from Activity respectively.

B) To dynamically change the title, you can use cust_title to obtain TextView and set it.

  

End

I have been busy some time ago (in fact, I am also very busy now--#). This problem has been bothering me for a long time and is finally solved.

Related Article

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.