- package com.zhf.ActivityInLayoutDemo;
- import java.util.HashMap;
- import java.util.Map;
- import Android.app.ActivityGroup;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.LinearLayout;
- /**
- * 布局中嵌入Activity例子
- * @author administrator
- *
- */
- public class ActivityInLayoutDemoActivity extends ActivityGroup {
- private LinearLayout container;
- private Button click1;
- private Button click2;
- /**
- * 子View管理
- */
- private Map<String, View> childViews = new HashMap<String, View>();
- private String currentTag;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- container = (LinearLayout) findViewById(R.id.container);
- click1 = (Button) findViewById(R.id.click1);
- click2 = (Button) findViewById(R.id.click2);
- click1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- startActivity("activity1", new Intent(ActivityInLayoutDemoActivity.this, Activity1.class));
- }
- });
- click2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- startActivity("activity2", new Intent(ActivityInLayoutDemoActivity.this, Activity2.class));
- }
- });
- }
- /**
- * 加载子Activity
- *
- * @param tag
- * @param intent
- */
- private void startActivity(String tag, Intent intent) {
- if (currentTag != null) {
- View currentView = childViews.get(currentTag);
- if (currentView != null)
- currentView.setVisibility(View.GONE);
- }
- currentTag = tag;
- View originView = childViews.get(tag);
- final Window window = getLocalActivityManager().startActivity(tag,
- intent);
- final View decorView = window.getDecorView();
- if (decorView != originView && originView != null) {
- if (originView.getParent() != null)
- ((ViewGroup) originView.getParent()).removeView(originView);
- }
- childViews.put(tag, decorView);
- if (decorView != null) {
- decorView.setVisibility(View.VISIBLE);
- decorView.setFocusableInTouchMode(true);
- ((ViewGroup) decorView)
- .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
- if (decorView.getParent() == null) {
- container.addView(decorView,
- new LinearLayout.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT,
- ViewGroup.LayoutParams.FILL_PARENT));
- }
- decorView.requestFocus();
- }
- }
- }
activity嵌套
最新推荐文章于 2021-05-29 03:06:37 发布