在一个页面中显示多个activity

今天项目要求在一个页面中显示多个activity,在网上差了些资料,整理并写了个demo测试了,功能实现了。

首先主界面要用GroupActivity,他是系统中继承Activity的一个类,详细的查看sdk文档。关键就是用到了一个LocationActivityManager类,对GroupActivity的子类进行管理。

MainActivity.javas实现:

package com.android.qiu.activitygroup;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends ActivityGroup {
    private final String TAG = "MainActivity";
    private RadioGroup radiogroup;
    private RadioButton radioBtn1;
    private RadioButton radioBtn2;
    private LocalActivityManager activitymanager;
    private LinearLayout layout_load;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        radiogroup = (RadioGroup) findViewById(R.id.mainRadioGroup);
        radioBtn1 = (RadioButton) findViewById(R.id.radioBtn1);
        radioBtn2 = (RadioButton) findViewById(R.id.radioBtn2);
        activitymanager = getLocalActivityManager();
        layout_load = (LinearLayout) findViewById(R.id.LinearLayout);
        radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if (checkedId == radioBtn1.getId()) {
                    Intent intent = new Intent(MainActivity.this,
                            ActivityA.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    Window w = activitymanager.startActivity("A", intent);
                    View v = w.getDecorView();
                    layout_load.removeAllViews();
                    layout_load.setPadding(5, 5, 5, 5);
//                    layout_load.addView(v);
                    layout_load
                            .addView(v, new LayoutParams(
                                    LayoutParams.FILL_PARENT,
                                    LayoutParams.WRAP_CONTENT));
                    Log.v(TAG, "******JJJJ");/*
                                             * layout_load.addView(v, new
                                             * RelativeLayout.LayoutParams(
                                             * RelativeLayout
                                             * .LayoutParams.FILL_PARENT,
                                             * LinearLayout
                                             * .LayoutParams.FILL_PARENT));
                                             */
                } else if (checkedId == radioBtn2.getId()) {
                    Intent intent2 = new Intent(MainActivity.this,
                            ActivityB.class);
                    intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    Window w2 = activitymanager.startActivity("B", intent2);
                    View v2 = w2.getDecorView();
                    layout_load.removeAllViews();
                    layout_load.setPadding(5, 5, 5, 5);
//                    layout_load.addView(v2);
                    layout_load
                    .addView(v2, new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    Log.v(TAG, "******GGGG");
                }
            }
        });
    }

}

 

main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout android:id="@+id/relativelayout"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <LinearLayout android:id="@+id/LinearLayout"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />

        <RadioGroup android:id="@+id/mainRadioGroup"
            android:layout_alignParentBottom="true" android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content">
            <RadioButton android:id="@+id/radioBtn1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="页面一"></RadioButton>
            <RadioButton android:id="@+id/radioBtn2"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_marginLeft="20dip" android:text="页面二"></RadioButton>
        </RadioGroup>
    </RelativeLayout>
</LinearLayout>

 

ActivityA.java 实现:

package com.android.qiu.activitygroup;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityA extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        final TextView tv = (TextView)findViewById(R.id.tv2);
        Button change = (Button)findViewById(R.id.changeBtn);
        change.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                tv.setText("在自己的activity中相应");
            }
        });
    }

}

 

layout.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:text="页面二"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv2"
    ></TextView>
    <Button android:id="@+id/changeBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="改变"
    ></Button>
</LinearLayout>

 

ActivityB.java实现:

package com.android.qiu.activitygroup;

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

public class ActivityB extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
    }

}

 

layout1.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:text="页面一"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ></TextView>
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值