`
trygood
  • 浏览: 76108 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用ActivityGroup来切换Activity和Layout

XML 
阅读更多
在一个主界面中做Activity切换一般都会用TabActivity,使用方便,Activity互相之间相对独立,但是可定制性不强,而且修改起来很麻烦。当然也可以把layout分开,把逻辑代码全写在主界面的逻辑代码中,但是很明显可维护性相当差,这里通过ActivityGroup来解决这个问题。 


 

要求点击底部不同图片按钮切换不同的Activity,并在中间显示Activity对应的ContentView 

二、 实现代码 


2.1  layout.xml 

Java代码  

1. <?xml version="1.0" encoding="utf-8"?>  
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3.     android:layout_width="fill_parent" android:orientation="vertical"  
4.     android:layout_height="fill_parent">  
5.     <LinearLayout android:gravity="center_horizontal"  
6.         android:background="@drawable/myinfor2" android:layout_width="fill_parent"  
7.         android:layout_height="wrap_content">  
8.         <TextView android:id="@+id/cust_title" android:textColor="@android:color/white"  
9.             android:textSize="28sp" android:text="模块1" android:layout_width="wrap_content"  
10.             android:layout_height="wrap_content"></TextView>  
11.     </LinearLayout>  
12.     <!-- 中间动态加载View -->  
13.     <ScrollView android:measureAllChildren="true" android:id="@+id/containerBody"  
14.         android:layout_weight="1" android:layout_height="fill_parent"  
15.         android:layout_width="fill_parent">  
16.     </ScrollView>  
17.     <LinearLayout android:background="@android:color/black"  
18.         android:layout_gravity="bottom" android:orientation="horizontal"  
19.         android:layout_width="fill_parent" android:layout_height="wrap_content">  
20.         <!-- 功能模块按钮1 -->  
21.         <ImageView android:id="@+id/btnModule1" android:src="@android:drawable/ic_dialog_dialer"  
22.             android:layout_marginLeft="7dp" android:layout_marginTop="3dp"  
23.             android:layout_marginBottom="3dp" android:layout_width="wrap_content"  
24.             android:layout_height="wrap_content" />  
25.         <!-- 功能模块按钮2 -->  
26.         <ImageView android:id="@+id/btnModule2" android:src="@android:drawable/ic_dialog_info"  
27.             android:layout_marginLeft="7dp" android:layout_marginTop="3dp"  
28.             android:layout_marginBottom="3dp" android:layout_width="wrap_content"  
29.             android:layout_height="wrap_content" />  
30.         <!-- 功能模块按钮3 -->  
31.         <ImageView android:id="@+id/btnModule3" android:src="@android:drawable/ic_dialog_alert"  
32.             android:layout_marginLeft="7dp" android:layout_marginTop="3dp"  
33.             android:layout_marginBottom="3dp" android:layout_width="wrap_content"  
34.             android:layout_height="wrap_content" />  
35.     </LinearLayout>  
36. </LinearLayout>  

2.2  TestView.java 
Java代码  

1. /** 
2.  * 使用ActivityGroup来切换Activity和Layout 
3.  * @author 农民伯伯 
4.  * @version 2010-9-7 
5.  *  
6.  */  
7. public class TestView extends ActivityGroup {  
8.   
9.     private ScrollView container = null;  
10.   
11.     @Override  
12.     protected void onCreate(Bundle savedInstanceState) {  
13.         super.onCreate(savedInstanceState);  
14.         // 隐藏标题栏  
15.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
16.         // 设置视图  
17.         setContentView(R.layout.layout);  
18.   
19.         container = (ScrollView) findViewById(R.id.containerBody);  
20.   
21.         // 模块1  
22.         ImageView btnModule1 = (ImageView) findViewById(R.id.btnModule1);  
23.         btnModule1.setOnClickListener(new OnClickListener() {  
24.             @Override  
25.             public void onClick(View v) {  
26.                 container.removeAllViews();  
27.                 container.addView(getLocalActivityManager().startActivity(  
28.                         "Module1",  
29.                         new Intent(TestView.this, ModuleView1.class)  
30.                                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
31.                         .getDecorView());  
32.             }  
33.         });  
34.   
35.         // 模块2  
36.         ImageView btnModule2 = (ImageView) findViewById(R.id.btnModule2);  
37.         btnModule2.setOnClickListener(new OnClickListener() {  
38.             @Override  
39.             public void onClick(View v) {  
40.                 container.removeAllViews();  
41.                 container.addView(getLocalActivityManager().startActivity(  
42.                         "Module2",  
43.                         new Intent(TestView.this, ModuleView2.class)  
44.                                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
45.                         .getDecorView());  
46.             }  
47.         });  
48.   
49.         // 模块3  
50.         ImageView btnModule3 = (ImageView) findViewById(R.id.btnModule3);  
51.         btnModule3.setOnClickListener(new OnClickListener() {  
52.             @Override  
53.             public void onClick(View v) {  
54.                 container.removeAllViews();  
55.                 container.addView(getLocalActivityManager().startActivity(  
56.                         "Module3",  
57.                         new Intent(TestView.this, ModuleView3.class)  
58.                                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
59.                         .getDecorView());  
60.             }  
61.         });  
62.     }  
63. }  
  • 大小: 54 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics