1. 镶嵌View的主ActivityGroup
package com.isomobile.widgets;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends ActivityGroup implements View.OnClickListener {
private final static Class<?>[] sActivityClasses = {
Activity1.class, Activity2.class, Activity3.class, Activity4.class, Activity5.class
};
private final static int[] sResIds = {
R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5
};
private final static String[] sActivityIds = {
"Activity1", "Activity2", "Activity3", "Activity4", "Activity5"
};
private RelativeLayout mViewContainer;
private Button[] mBtns = new Button[sResIds.length];
private int mCurId = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews() {
mViewContainer = (RelativeLayout) findViewById(R.id.container);
final Button[] btns = mBtns;
for(int i=0; i< btns.length; i++) {
btns[i] = (Button) findViewById(sResIds[i]);
btns[i].setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
final int id = v.getId();
if(mCurId == id) {
return ;
}
mCurId = id;
processViews(id);
}
private void processViews(int rid) {
mViewContainer.removeAllViews();
final int index = getButtonIndex(rid);
final View tempView = getLocalActivityManager().startActivity(sActivityIds[index],
new Intent(this, sActivityClasses[index]).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
mViewContainer.addView(tempView);
}
private int getButtonIndex(int rid) {
final int length = sResIds.length;
for (int i = 0; i < length; i++) {
if (rid == sResIds[i]) {
return i;
}
}
return 0;
}
}
2. 其它子Activity(Activity1~5代码基本雷同这里只给出一个):
package com.isomobile.widgets;
import android.app.Activity;
import android.os.Bundle;
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1_layout);
}
}
3. Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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/container"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</RelativeLayout>
<include
layout="@layout/bottom_buttons" />
</RelativeLayout>
4. bottom_buttons.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="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="5.0"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_gravity="bottom"
android:gravity="bottom"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
<Button
android:id="@+id/btn2"
android:layout_gravity="bottom"
android:gravity="bottom"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
<Button
android:id="@+id/btn3"
android:layout_gravity="bottom"
android:gravity="bottom"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
<Button
android:id="@+id/btn4"
android:layout_gravity="bottom"
android:gravity="bottom"
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btn5"
android:layout_gravity="bottom"
android:gravity="bottom"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</LinearLayout>
效果图片如下: