Android使用ActivityGroup来切换Activity和Layout

2024-03-10 17:18

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

前言

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


文章

  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* */
public class TestView extends ActivityGroup {private ScrollView container = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 隐藏标题栏requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置视图setContentView(R.layout.layout);container = (ScrollView) findViewById(R.id.containerBody);// 模块1ImageView btnModule1 = (ImageView) findViewById(R.id.btnModule1);btnModule1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {container.removeAllViews();container.addView(getLocalActivityManager().startActivity("Module1",new Intent(TestView.this, ModuleView1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());}});// 模块2ImageView btnModule2 = (ImageView) findViewById(R.id.btnModule2);btnModule2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {container.removeAllViews();container.addView(getLocalActivityManager().startActivity("Module2",new Intent(TestView.this, ModuleView2.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());}});// 模块3ImageView btnModule3 = (ImageView) findViewById(R.id.btnModule3);btnModule3.setOnClickListener(new OnClickListener() {@Overridepublic 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进行设置。 

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



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

相关文章

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me