前言

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

声明

  欢迎转载,但请保留文章原始出处:)

  农民伯伯: http://over140.blog.51cto.com/

文章

  1.  Android: TabActivity Nested Activities

  2.  Android ActivityGroup的使用代码将子activty 的layout加入到主activity中

正文

  一、效果图

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

  二、 实现代码

    2.1  layout.xml

<? xmlversion="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 ="模块1" android:layout_width ="wrap_content"
android:layout_height
="wrap_content" ></ TextView >
</ LinearLayout >
<!-- 中间动态加载View -->
< 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" >
<!-- 功能模块按钮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" />
<!-- 功能模块按钮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" />
<!-- 功能模块按钮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

/**
*使用ActivityGroup来切换Activity和Layout
*
@author 农民伯伯
*
@version 2010-9-7
*
*/
public class TestView extends ActivityGroup{

private ScrollViewcontainer = null ;

@Override
protected void onCreate(BundlesavedInstanceState){
super .onCreate(savedInstanceState);
// 隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 设置视图
setContentView(R.layout.layout);

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

// 模块1
ImageViewbtnModule1 = (ImageView)findViewById(R.id.btnModule1);
btnModule1.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(Viewv){
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
" Module1 " ,
new Intent(TestView. this ,ModuleView1. class )
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});

// 模块2
ImageViewbtnModule2 = (ImageView)findViewById(R.id.btnModule2);
btnModule2.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(Viewv){
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
" Module2 " ,
new Intent(TestView. this ,ModuleView2. class )
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});

// 模块3
ImageViewbtnModule3 = (ImageView)findViewById(R.id.btnModule3);
btnModule3.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(Viewv){
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
" Module3 " ,
new Intent(TestView. this ,ModuleView3. class )
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});
}
}

    代码说明:

      a).  ModuleView1、ModuleView2 ModuleView3分别继承自Activity。

      b).  想动态改变标题可以通过cust_title获取TextView进行设置。

  

结束

   前段时间忙(其实现在也很忙 - - #),这个问题一直困扰心头,总算是解决,舒心 :)

更多相关文章

  1. Android的开源隐忧:品牌稀释 代码分裂
  2. linux和windows平台下,如何下载android sdk的源代码
  3. Android之替换APP字体——Typeface
  4. Android(安卓)Building System 总结
  5. Android(安卓)命名规范 (提高代码可以读性)
  6. android之webview无网络情况下简单处理
  7. Android高手进阶教程(二十五)之---Android(安卓)中的AIDL!!!
  8. Android项目源码混淆问题解决方法
  9. [Android] 开心消消乐代码(写的比较简单)

随机推荐

  1. mysql随机查询若干条数据
  2. spark sql scala 开发环境搭建 以及必要j
  3. SQL注入之重新认识
  4. Spring boot 和 mybatis 学习笔记3--动态
  5. 如何在oracle中为每个重复的id行获取一条
  6. 考研成绩查询系统(jsp、mysql、java)
  7. PHP学习笔记(三):mysqli_fetch_row和mysqli_
  8. Mysql查询时,对于数值型字段加单引号会引
  9. mysqlbinlog 查看二进制日志
  10. SQL Server 执行计划操作符详解(1)——断言