android-使用ActivityGroup来切换Activity和Layout - 随心

2024-02-26 05:48

本文主要是介绍android-使用ActivityGroup来切换Activity和Layout - 随心,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

[Android]使用ActivityGroup来切换Activity和Layout


前言

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

 

 

声明

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

    博客园:http://www.cnblogs.com

    农民伯伯: http://www.cnblogs.com/over140/ 

 

 

文章

  1.  Android: TabActivity Nested Activities

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

 

正文

  一、效果图

     

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

 

  二、 实现代码

    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 ="模块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  ScrollView container  =   null ;

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

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

        
//  模块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());
            }
        });

        
//  模块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());
            }
        });

        
//  模块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());
            }
        });
    }
}
复制代码

    代码说明:

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

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

转自: http://www.cnblogs.com/over140/archive/2010/09/07/1820876.html



这篇关于android-使用ActivityGroup来切换Activity和Layout - 随心的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/747855

相关文章

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib