activity嵌套

 
  1. package com.zhf.ActivityInLayoutDemo;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5. import Android.app.ActivityGroup;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.view.Window;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.LinearLayout;  
  14. /** 
  15.  * 布局中嵌入Activity例子 
  16.  * @author administrator 
  17.  * 
  18.  */  
  19. public class ActivityInLayoutDemoActivity extends ActivityGroup {  
  20.     private LinearLayout container;  
  21.     private Button click1;  
  22.     private Button click2;  
  23.     /** 
  24.      * 子View管理 
  25.      */  
  26.     private Map<String, View> childViews = new HashMap<String, View>();  
  27.     private String currentTag;  
  28.   
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.         container = (LinearLayout) findViewById(R.id.container);  
  34.         click1 = (Button) findViewById(R.id.click1);  
  35.         click2 = (Button) findViewById(R.id.click2);  
  36.         click1.setOnClickListener(new OnClickListener() {  
  37.   
  38.             @Override  
  39.             public void onClick(View arg0) {  
  40.                 startActivity("activity1"new Intent(ActivityInLayoutDemoActivity.this, Activity1.class));  
  41.             }  
  42.         });  
  43.   
  44.         click2.setOnClickListener(new OnClickListener() {  
  45.   
  46.             @Override  
  47.             public void onClick(View arg0) {  
  48.                 startActivity("activity2"new Intent(ActivityInLayoutDemoActivity.this, Activity2.class));  
  49.             }  
  50.         });  
  51.     }  
  52.   
  53.     /** 
  54.      * 加载子Activity 
  55.      *  
  56.      * @param tag 
  57.      * @param intent 
  58.      */  
  59.     private void startActivity(String tag, Intent intent) {  
  60.         if (currentTag != null) {  
  61.             View currentView = childViews.get(currentTag);  
  62.             if (currentView != null)  
  63.                 currentView.setVisibility(View.GONE);  
  64.         }  
  65.         currentTag = tag;  
  66.         View originView = childViews.get(tag);  
  67.         final Window window = getLocalActivityManager().startActivity(tag,  
  68.                 intent);  
  69.         final View decorView = window.getDecorView();  
  70.         if (decorView != originView && originView != null) {  
  71.             if (originView.getParent() != null)  
  72.                 ((ViewGroup) originView.getParent()).removeView(originView);  
  73.         }  
  74.         childViews.put(tag, decorView);  
  75.         if (decorView != null) {  
  76.             decorView.setVisibility(View.VISIBLE);  
  77.             decorView.setFocusableInTouchMode(true);  
  78.             ((ViewGroup) decorView)  
  79.                     .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);  
  80.             if (decorView.getParent() == null) {  
  81.                 container.addView(decorView,  
  82.                         new LinearLayout.LayoutParams(  
  83.                                 ViewGroup.LayoutParams.FILL_PARENT,  
  84.                                 ViewGroup.LayoutParams.FILL_PARENT));  
  85.             }  
  86.             decorView.requestFocus();  
  87.         }  
  88.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值