首页 > 代码库 > Android项目---ActivityGroup的使用

Android项目---ActivityGroup的使用

ActivityGroup在一年前已经被说明不用了,但是腾讯QQ,新浪微博上也不乏ActivityGroup的身影。所以,即使是过时的东西,也有学习的必要,当然项目中也是可以添加的。在网上找了一个博客,不过还是想自己理解一下其中的因原。http://www.cnblogs.com/answer1991/archive/2012/05/08/2489844.html

mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="0.0px" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:orientation="horizontal"
            android:layout_weight="1.0" />

        <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/radio_button0"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@android:drawable/ic_menu_call"
                android:text="按钮1" />

            <Button
                android:id="@+id/radio_button1"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@android:drawable/ic_menu_camera"
                android:text="按钮2" />

            <Button
                android:id="@+id/radio_button2"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@android:drawable/ic_menu_agenda"
                android:text="按钮3" />

            <Button
                android:id="@+id/radio_button3"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@android:drawable/ic_menu_delete"
                android:text="按钮4" />

            <Button
                android:id="@+id/radio_button4"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@android:drawable/ic_menu_help"
                android:text="按钮5" />
        </RadioGroup>
    </LinearLayout>

</LinearLayout>

其实这个layout的结构挺简单的:

功能代码:

1.定义一个生成Activity的工厂

protected void setContainerView(String activityName,
            Class<?> activityClassTye) {
        localActivityManager = getLocalActivityManager();
        viewgroup = getContainer();
        viewgroup.removeAllViews();
        Activity containerActivity = localActivityManager
                .getActivity(activityName);
        if (containerActivity == null) {
            localActivityManager.startActivity(activityName, new Intent(this,
                    activityClassTye).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        }
        viewgroup.addView(localActivityManager.getActivity(activityName)
                .getWindow().getDecorView(), new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }

 

2.找到自定义id加在Activity的View

protected ViewGroup getContainer() {
        return (ViewGroup) findViewById(R.id.container);
    }

 

3.添加进入子Activity的intent事件

setContainerView(activity_id0, Activity0.class);//设置默认的activity显示

4.添加按钮事件就可以在主Activity中随意切换到子Activity中了。

 

效果图: