歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android之在Layout中嵌入Activity

有些復雜的布局的實現,這裡可以提供一個思路,在布局中嵌入自己的Activity,直接貼源碼:

1.ActivityInLayoutDemoActivity.java:

 

  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.     }  
  89. }  

2.效果圖

Copyright © Linux教程網 All Rights Reserved