自定义View仿TabHost的实现

简介:

Activity1如下:

[java]  view plain copy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class Activity1 extends Activity{  
  5.      @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         //设置setTitle应该在setTitle之前  
  9.         //否则不起作用  
  10.         setTitle("Activity1");  
  11.         setContentView(R.layout.activity_1);  
  12.     }  
  13. }  


Activity2如下:

[java]  view plain copy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class Activity2 extends Activity{  
  5.      @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         //设置setTitle应该在setTitle之前  
  9.         //否则不起作用  
  10.         setTitle("Activity2");  
  11.         setContentView(R.layout.activity_2);  
  12.     }  
  13. }  


Activity3如下:

[java]  view plain copy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class Activity3 extends Activity{  
  5.      @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         //设置setTitle应该在setTitle之前  
  9.         //否则不起作用  
  10.         setTitle("Activity3");  
  11.         setContentView(R.layout.activity_3);  
  12.     }  
  13. }  


Activity4如下:

[java]  view plain copy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class Activity4 extends Activity{  
  5.      @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         //设置setTitle应该在setTitle之前  
  9.         //否则不起作用  
  10.         setTitle("Activity4");  
  11.         setContentView(R.layout.activity_4);  
  12.     }  
  13. }  


BottomMenuView如下:

[java]  view plain copy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.util.AttributeSet;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.LinearLayout;  
  10. //自定义View.仿TabHost的实现  
  11. public class BottomMenuView extends LinearLayout{  
  12.     private Context mContext;  
  13.     private LinearLayout mLinearLayout1;  
  14.     private LinearLayout mLinearLayout2;  
  15.     private LinearLayout mLinearLayout3;  
  16.     private LinearLayout mLinearLayout4;  
  17.     private String mCurrentActivityTitle;  
  18.     private final static String ACTIVITY_NAME_1="Activity1";  
  19.     private final static String ACTIVITY_NAME_2="Activity2";  
  20.     private final static String ACTIVITY_NAME_3="Activity3";  
  21.     private final static String ACTIVITY_NAME_4="Activity4";  
  22.     public BottomMenuView(Context context, AttributeSet attrs) {  
  23.         super(context, attrs);  
  24.         mContext=context;  
  25.         initView();  
  26.     }  
  27.     private void initView(){  
  28.         View bottomMenuView=  
  29.         LayoutInflater.from(mContext).inflate(R.layout.bottommenu, nullfalse);  
  30.         ViewGroup.LayoutParams params=  
  31.         new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);  
  32.         this.addView(bottomMenuView,params);  
  33.           
  34.         mLinearLayout1=(LinearLayout)  
  35.         bottomMenuView.findViewById(R.id.menu_LinearLayout1);  
  36.         mLinearLayout2=(LinearLayout)  
  37.         bottomMenuView.findViewById(R.id.menu_LinearLayout2);  
  38.         mLinearLayout3=(LinearLayout)  
  39.         bottomMenuView.findViewById(R.id.menu_LinearLayout3);  
  40.         mLinearLayout4=(LinearLayout)  
  41.         bottomMenuView.findViewById(R.id.menu_LinearLayout4);  
  42.           
  43.         BottomMenuOnClickListener clickListener=new BottomMenuOnClickListener();  
  44.         mLinearLayout1.setOnClickListener(clickListener);  
  45.         mLinearLayout2.setOnClickListener(clickListener);  
  46.         mLinearLayout3.setOnClickListener(clickListener);  
  47.         mLinearLayout4.setOnClickListener(clickListener);  
  48.         setEveryMenuStatus();  
  49.     }  
  50.       
  51.     /** 
  52.      *  
  53.      * 1 在每个menu对应的Activity中设置其Title 
  54.      * 2 在此判断当前显示是哪一个Activity.使得 
  55.      *   当前Activity对应的Menu不可再次点击. 
  56.      */  
  57.     private void setEveryMenuStatus(){  
  58.         mCurrentActivityTitle=(String) ((Activity)mContext).getTitle();  
  59.         if (mCurrentActivityTitle.equals(ACTIVITY_NAME_1)) {  
  60.             mLinearLayout1.setClickable(false);  
  61.         }else if (mCurrentActivityTitle.equals(ACTIVITY_NAME_2)) {  
  62.             mLinearLayout2.setClickable(false);  
  63.         }else if (mCurrentActivityTitle.equals(ACTIVITY_NAME_3)) {  
  64.             mLinearLayout3.setClickable(false);  
  65.         }else if (mCurrentActivityTitle.equals(ACTIVITY_NAME_4)) {  
  66.             mLinearLayout4.setClickable(false);  
  67.         }  
  68.     }  
  69.       
  70.     private class BottomMenuOnClickListener implements OnClickListener{  
  71.         public void onClick(View v) {  
  72.             switch (v.getId()) {  
  73.             case R.id.menu_LinearLayout1:  
  74.                 Intent intent1=new Intent(mContext, Activity1.class);  
  75.                 mContext.startActivity(intent1);  
  76.                 break;  
  77.             case R.id.menu_LinearLayout2:  
  78.                 Intent intent2=new Intent(mContext, Activity2.class);  
  79.                 mContext.startActivity(intent2);  
  80.                 break;  
  81.             case R.id.menu_LinearLayout3:  
  82.                 Intent intent3=new Intent(mContext, Activity3.class);  
  83.                 mContext.startActivity(intent3);  
  84.                 break;  
  85.             case R.id.menu_LinearLayout4:  
  86.                 Intent intent4=new Intent(mContext, Activity4.class);  
  87.                 mContext.startActivity(intent4);  
  88.                 break;  
  89.             default:  
  90.                 break;  
  91.             }  
  92.               
  93.         }  
  94.           
  95.     }  
  96.   
  97. }  

activity_1.xml如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.       
  7.     <TextView   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="Activity 1"  
  12.         android:textSize="50sp"  
  13.     />  
  14.       <cn.com.BottomMenuView  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="70dip"  
  17.         android:layout_alignParentBottom="true"  
  18.      />  
  19. </RelativeLayout>  


activity_2.xml如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.       
  7.     <TextView   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="Activity 2"  
  12.         android:textSize="50sp"  
  13.     />  
  14.       <cn.com.BottomMenuView  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="70dip"  
  17.         android:layout_alignParentBottom="true"  
  18.      />  
  19. </RelativeLayout>  

 

activity_3.xml如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.       
  7.     <TextView   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="Activity 3"  
  12.         android:textSize="50sp"  
  13.     />  
  14.       <cn.com.BottomMenuView  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="70dip"  
  17.         android:layout_alignParentBottom="true"  
  18.      />  
  19. </RelativeLayout>  


activity_4.xml如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.       
  7.     <TextView   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="Activity 4"  
  12.         android:textSize="50sp"  
  13.     />  
  14.       <cn.com.BottomMenuView  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="70dip"  
  17.         android:layout_alignParentBottom="true"  
  18.      />  
  19. </RelativeLayout>  


bottommenu.xml如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="70dip"  
  5.     android:orientation="horizontal" >  
  6.     <LinearLayout   
  7.         android:id="@+id/menu_LinearLayout1"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:clickable="true"  
  11.         android:background="@drawable/photo1"  
  12.         android:layout_weight="1"  
  13.     />  
  14.     <LinearLayout   
  15.         android:id="@+id/menu_LinearLayout2"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:clickable="true"  
  19.         android:background="@drawable/photo2"  
  20.         android:layout_weight="1"  
  21.     />  
  22.     <LinearLayout   
  23.         android:id="@+id/menu_LinearLayout3"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:clickable="true"  
  27.         android:background="@drawable/photo3"  
  28.         android:layout_weight="1"  
  29.     />  
  30.     <LinearLayout   
  31.         android:id="@+id/menu_LinearLayout4"  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:clickable="true"  
  35.         android:background="@drawable/photo4"  
  36.         android:layout_weight="1"  
  37.     />  
  38.       
  39. </LinearLayout>  


manifest.xml如下:

[html]  view plain copy
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="cn.com"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="15" />  
  9.   
  10.     <application  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.          <activity  
  15.             android:name=".Activity1">  
  16.             <intent-filter>  
  17.                 <action android:name="android.intent.action.MAIN" />  
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.          </activity>  
  21.           <activity  
  22.             android:name=".Activity2">  
  23.          </activity>  
  24.           <activity  
  25.             android:name=".Activity3">  
  26.          </activity>  
  27.           <activity  
  28.             android:name=".Activity4">  
  29.          </activity>  
  30.     </application>  
  31.   
  32. </manifest>  
相关文章
|
7天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
6天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
316 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
18天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1331 8
|
5天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
17天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1411 87
|
6天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
312 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
5天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
6天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
253 82
2025年阿里云域名备案流程(新手图文详细流程)