Android组件通信——ActivityGroup(二十五)

news2025/6/25 15:21:02

1. ActivityGroup

1.1 知识点

(1)了解ActivityGroup的作用;

(2)使用ActivityGroup进行复杂标签菜单的实现;

(3)使用PopupWindow组件实现弹出菜单组件开发;

1.2 具体内容

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ActivityGroupActivity" >

    <LinearLayout 
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        >
        <TextView android:id="@+id/cust_title"
                  android:textSize="28sp"
                  android:text="ActivityGroup实现分页导航"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
        /> 
    </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_height="wrap_content"
        android:layout_width="fill_parent"
        >
        <!-- 导航按钮1 -->
        <ImageView 
            android:id="@+id/img1"
            android:src="@android:drawable/ic_dialog_dialer"
            android:layout_marginLeft="7dp" 
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            />
         <!-- 导航按钮2 -->
        <ImageView 
            android:id="@+id/img2"
            android:src="@android:drawable/ic_dialog_info"
            android:layout_marginLeft="7dp" 
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            />
         <!-- 导航按钮3 -->
        <ImageView 
            android:id="@+id/img3"
            android:src="@android:drawable/ic_dialog_alert"
            android:layout_marginLeft="7dp" 
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            />
    </LinearLayout>
    
</LinearLayout>

package com.example.activitygroupproject;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ScrollView;

public class ActivityGroupActivity extends ActivityGroup {
    ScrollView container =null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
		setContentView(R.layout.activity_activity_group);
		container = (ScrollView) super.findViewById(R.id.containerBody);
		//导航1
		ImageView img1= (ImageView) super.findViewById(R.id.img1);
		img1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				container.removeAllViews();//清空子View
				container.addView(getLocalActivityManager().startActivity("Module1", 
						new Intent(ActivityGroupActivity.this,ModuleView1.class)
				.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
			}
		});
		//导航2
				ImageView img2= (ImageView) super.findViewById(R.id.img2);
				img2.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						container.removeAllViews();//清空子View
						container.addView(getLocalActivityManager().startActivity("Module2", 
								new Intent(ActivityGroupActivity.this,ModuleView2.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
					}
				});
				//导航3
				ImageView img3= (ImageView) super.findViewById(R.id.img3);
				img3.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						container.removeAllViews();//清空子View
						container.addView(getLocalActivityManager().startActivity("Module3", 
								new Intent(ActivityGroupActivity.this,ModuleView3.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
					}
				});
	}


}

下面是子Activity的布局和文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ModuleView1" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一个Module" />

</RelativeLayout>

package com.example.activitygroupproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class ModuleView1 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_module_view1);
	}


}

共有三个子Activity,其余两个类似,就只写一个。

以下实现目前非常流行的标签页实现形式FragmentTabHost+ViewPager。

主布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".FragmentTabHostActivity" >
   
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    
    <FrameLayout
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <FrameLayout 
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"
            >
            
        </FrameLayout>
        
        
        
    </android.support.v4.app.FragmentTabHost>
        
</LinearLayout>

Activity:

package com.example.fragmenttabhost;

import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

public class FragmentTabHostActivity extends FragmentActivity {
	FragmentTabHost mTabHost = null;
    LayoutInflater layoutInflater = null;
    Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class};
    int mImageViewArray[] = {android.R.drawable.ic_dialog_dialer,android.R.drawable.ic_dialog_info,android.R.drawable.ic_dialog_alert};
    String mTextViewArray[] = {"首页","消息","好友"};
    ViewPager vp;
    List<Fragment> list = new ArrayList<Fragment>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment_tab_host);
		//实例化组件
		initView();
		initPager();
	}

    public void initView(){
    	vp = (ViewPager) super.findViewById(R.id.pager);
    	vp.setOnPageChangeListener(new ViewPagerListener());
    	layoutInflater = LayoutInflater.from(this);//实例化布局对象
    	mTabHost = (FragmentTabHost) super.findViewById(android.R.id.tabhost);
    	mTabHost.setup(this,getSupportFragmentManager(),R.id.pager);//实例化FragmentTabHost对象
    	mTabHost.setOnTabChangedListener(new TabHostListener());
    	int count = fragmentArray.length;//获取子tab的个数
    	for(int i= 0;i<count;i++){
    		//为每一个Tab按钮设置图标文字和内容
    		TabSpec tabSpec = mTabHost.newTabSpec(mTextViewArray[i]).setIndicator(getTabItemView(i));
    		mTabHost.addTab(tabSpec,fragmentArray[i],null);//将子tab添加进TabHost
    		//设置按钮的背景
    		mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(color.background_dark);
    	}
    }
    
    
    private void initPager(){
    	FragmentPage1 p1 = new FragmentPage1();
    	FragmentPage2 p2 = new FragmentPage2();
    	FragmentPage3 p3 = new FragmentPage3();
    	list.add(p1);
    	list.add(p2);
    	list.add(p3);
    	vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }
    
    private View getTabItemView(int index){
    	View view = layoutInflater.inflate(R.layout.tabspec_layout, null);
    	ImageView img = (ImageView) view.findViewById(R.id.img);
    	img.setImageResource(mImageViewArray[index]);
    	TextView tv = (TextView) view.findViewById(R.id.tv);
    	tv.setText(mTextViewArray[index]);
    	return view;
    }
    
    class ViewPagerListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int arg0) {//根据焦点来确认切换到那个Tab
			TabWidget  widget = mTabHost.getTabWidget();
			int oldFocusability = widget.getDescendantFocusability();
			widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
			mTabHost.setCurrentTab(arg0);
			widget.setDescendantFocusability(oldFocusability);
		}
    	
    }
    
    class TabHostListener implements OnTabChangeListener{

		@Override
		public void onTabChanged(String tabId) {
			int position = mTabHost.getCurrentTab();
			vp.setCurrentItem(position);
		}}
    
    class MyAdapter extends FragmentPagerAdapter{

		public MyAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}
    	
    }
}

单个标签布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    
    <ImageView 
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dp"
        />
    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        android:textColor="#FFFFFF"
        />

</LinearLayout>

单个fragment:

package com.example.fragmenttabhost;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentPage1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
		return inflater.inflate(R.layout.fragment, null);
    }
}

单个fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"
        />
    
    
</LinearLayout>

1.3 小结

(1)ActivityGroup可以让多个Activity在一个屏幕上集中显示;

(2)通过PopupWindow组件可以实现弹出菜单的功能。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1085935.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

17其他内置方法——信息格式化操作

其他内置方法信息格式化操作有两个&#xff0c;一个是__str__,一个是__repr__ 目录 1、__str__ ​编辑 触发方式有两种&#xff0c;一种是通过print&#xff08;p&#xff09;还有一种是打印str&#xff08;p1&#xff09; 2、__repr__:触发方式也有这两种 一种是直接打印…

Mysql高级——事务(1)

事务基础知识 1. 数据库事务概述 1.1 存储引擎支持情况 SHOW ENGINES 命令来查看当前 MySQL 支持的存储引擎都有哪些&#xff0c;以及这些存储引擎是否支持事务。 能看出在 MySQL 中&#xff0c;只有InnoDB 是支持事务的。 1.2 基本概念 **事务&#xff1a;**一组逻辑操作…

iMazing2023免费版苹果iPhone手机备份应用软件

iMazing是一款功能强大的苹果手机备份软件&#xff0c;它可通过备份功能将通讯录备份到电脑上&#xff0c;并在电脑端iMazing“通讯录”功能中随时查看和导出联系人信息。它自带Wi-Fi自动备份功能&#xff0c;能够保证通讯录备份数据是一直在动态更新的&#xff0c;防止手机中新…

去图片里面的水印怎么去?三个小妙招分享给你

当我们在网上搜集图片素材时&#xff0c;经常会遇到图片上有平台水印的情况&#xff0c;这是一个令人头疼的情况。这些水印可能会妨碍我们的创作&#xff0c;限制了素材的使用&#xff0c;那么去图片里面的水印怎么去呢&#xff1f;别担心今天我来分享一些非常实用的技巧&#…

Hydra参数

kali的hyda参数 参数&#xff1a; hydra [[[-l LOGIN|-L FILE] [-p PASS|-P FILE]] | [-C FILE]] [-e ns][-o FILE] [-t TASKS] [-M FILE [-T TASKS]] [-w TIME] [-f] [-s PORT] [-S] [-vV] server service [OPT] -R 继续从上一次进度接着破解。 -S 采用SSL链接。 -s PORT 可通…

windows10系统-15-markdown编辑器和文本复制工具Textify

1 markdown编辑器 Markdown是一种轻量级标记语言&#xff0c;创始人为约翰格鲁伯。 它允许人们使用易读易写的纯文本格式编写文档&#xff0c;然后转换成有效的XHTML&#xff08;或者HTML&#xff09;文档。这种语言吸收了很多在电子邮件中已有的纯文本标记的特性。 1.1 Typo…

Java每日笔试题错题分析(4)

Java每日笔试题错题分析&#xff08;4&#xff09; 一、错题知识点前瞻第1题第2题第3题第4题第5题 二、错题展示及其解析第1题第2题第3题第4题第5题 一、错题知识点前瞻 第1题 String声明变量在jvm中的存储方法 1&#xff0c;字符串在java中存储在字符串常量区中 2&#xff0c…

Unity编辑器从PC平台切换到Android平台下 Addressable 加载模型出现粉红色,类似于材质丢失的问题

Unity编辑器在PC平台下使用Addressable加载打包好的Cube&#xff0c;运行发现能正常显示。 而在切换到Android平台下&#xff0c;使用Addressable时加载AB包&#xff0c;生成Cube对象时&#xff0c;Cube模型呈现粉红色&#xff0c;出现类似材质丢失的问题。如下图所示。 这是…

Dubbo-SPI机制

1、Java的SPI机制 SPI的全称是Service Provider Interface&#xff0c;是JDK内置的动态加载实现扩展点的机制&#xff0c;通过SPI可以动态获取接口的实现类&#xff0c;属于一种设计理念。 系统设计的各个抽象&#xff0c;往往有很多不同的实现方案&#xff0c;在面向的对象的…

EOF() | BOF()相关题目解析

题目 设当前数据库有10条记录(记录未进行任何索引)&#xff0c;在下列3种情况下&#xff0c;当前记录号为1时&#xff1a;EOF()为真时&#xff1b;BOF()为真时&#xff0c;命令RECN()的结果分别是______。 A&#xff0e;1,11,1B&#xff0e;1,10,1C&#xff0e;1,11,0D&#xf…

Verilog功能模块——同步FIFO

前言 FIFO功能模块分两篇文章&#xff0c;本篇为同步FIFO&#xff0c;另一篇为异步FIFO&#xff0c;传送门&#xff1a; Verilog功能模块——异步FIFO-CSDN博客 同步FIFO实现起来是异步FIFO的简化版&#xff0c;所以&#xff0c;本博文不再介绍FIFO实现原理&#xff0c;感兴趣…

2023年中国电子白板市场规模、竞争格局及应用领域市场结构[图]

电子白板作为新型教育手段&#xff0c;如果合理地运用到现代教育活动中&#xff0c;使其自身的重视功能高效发挥出来&#xff0c;就能够极大地提升教育活动开展的顺利程度&#xff0c;加深学生对知识点的理解与把握&#xff0c;充分尊重学生是学习主体的地位&#xff0c;将保障…

【算法优选】 二分查找专题——贰

文章目录 &#x1f60e;前言&#x1f332;[山脉数组的峰顶索引](https://leetcode.cn/problems/peak-index-in-a-mountain-array/)&#x1f6a9;题目描述&#xff1a;&#x1f6a9;算法思路&#x1f6a9;代码实现&#xff1a; &#x1f334;[寻找峰值](https://leetcode.cn/pro…

Linus - make命令 和 makefile

make命令和 makefile 如果之前用过 vim 的话&#xff0c;应该会对 vim 又爱又恨吧&#xff0c;刚开始使用感觉非常的别扭&#xff0c;因为这种编写代码的方式&#xff0c;和在 windows 当中用图形化界面的方式编写代码的方式差别是不是很大。当你把vim 用熟悉的之后&#xff0…

苹果的数据怎么传输到电脑上?这五种方法轻松实现!

在日常使用苹果设备时&#xff0c;我们经常需要将重要的数据传输到电脑上进行备份或处理&#xff0c;那么苹果的数据怎么传输到电脑上呢&#xff1f;接下来&#xff0c;本文将为您提供多种传输的方法&#xff0c;帮助您将苹果设备上的数据轻松传输到电脑上。 方法一、使用iTun…

如何在雷电模拟器上安装Magisk并加载movecert模块抓https包(二)

接来下在PC端安装和配置Charles&#xff0c;方法同下面链接&#xff0c;不再赘述。在模拟器上安装magisk实现Charles抓https包&#xff08;二&#xff09;_小小爬虾的博客-CSDN博客 一、记录下本机IP和代理端口 二、在手机模拟器上设置代理192.168.31.71:8888&#xff0c;设置…

VScode商店无法访问

下面的方法也许对你没用&#xff0c;也许也有用&#xff0c;但是尝试一下不会有任何副作用。 步骤一&#xff1a; 步骤二&#xff1a;在Proxy代理设置中复制输入 http://127.0.0.1:8080 步骤三&#xff1a;关闭软件&#xff0c;再打开VScode&#xff0c;把http://127.0.0.1:8…

Typora安装教程

Typora 安装教程 安装 官网最新版 自行官网下载 社区版&#xff08;老版本&#xff0c;附带激活码&#xff09; 链接: https://pan.baidu.com/s/1t_3o3Xi7x09_8G1jpQYIvg?pwdmeyf 提取码: meyf 复制这段内容后打开百度网盘手机App&#xff0c;操作更方便哦 将百度云盘下…

单目标应用:火鹰优化算法(Fire Hawk Optimizer,FHO)求解微电网优化--提供MATLAB代码

一、火鹰优化算法FHO 火鹰优化算法&#xff08;Fire Hawk Optimizer&#xff0c;FHO&#xff09;由Mahdi Azizi等人于2022年提出&#xff0c;该算法性能高效&#xff0c;思路新颖。 单目标优化&#xff1a;火鹰优化算法&#xff08;Fire Hawk Optimizer&#xff0c;FHO&#…

【人工智能数学基础】几何解释——最小二乘法

先来一组对应关系&#xff1a; 一、使用拟合。 1.1代数计算 拟合后误差为&#xff0c;要找到一个a&#xff0c;使得 的和最小&#xff0c;计算 &#xff0c;用 f(a) 表示&#xff1a; 带入数据即可得到 易得f(a)最小时的a值。 1.2解超定方程组 用向量表示x和y&#xff1a…