像UC浏览器下面一排的功能按钮,当你点击一个,跳转activity,实际上这排按钮是不应该动的,因为每一页都需要这排按钮。于是,我们就需要只跳转上面部份,有点像HTML的iframe,于是ActivityGroup帮我们实现了这一功能。
有三个activity和三个布局文件。其中,main是主界面,activity1和activity2是要跳转的两个页面。
下面贴上完整代码:
main.java
package wzq.android.three;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
public class main extends ActivityGroup {
private Button button1;
private Button button2;
private LinearLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
container = (LinearLayout) findViewById(R.id.container);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
container.removeAllViews();
Intent intent = null;
intent = new Intent(main.this,TestActivity1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window subActivity = getLocalActivityManager().startActivity("subActivity", intent);
container.addView(subActivity.getDecorView(),LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
}
);
button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
container.removeAllViews();
Intent intent = null;
intent = new Intent(main.this,TestActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window subActivity = getLocalActivityManager().startActivity("subActivity", intent);
container.addView(subActivity.getDecorView(),LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
}
);
init();
}
private void init(){
container.removeAllViews();
Intent intent = null;
intent = new Intent(main.this,TestActivity1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window subActivity = getLocalActivityManager().startActivity("subActivity", intent);
container.addView(subActivity.getDecorView(),LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
}
TestActivity1.java
package wzq.android.three;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity1 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ac1);
}
}
TestActivity2.java
package wzq.android.three;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ac2);
}
}
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">
<LinearLayout android:id="@+id/container" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="100dip"
android:background="#0000ff">
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="窗体1" />
<Button android:id="@+id/button2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="窗体2" />
</LinearLayout>
</LinearLayout>
ac1.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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="aaaaaaaa111111111111"
/>
</LinearLayout>
ac2.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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="aaaaaaaa22222222222"
/>
</LinearLayout>
顺便补充一下intent flag方面的知识,我抄来的
一、Activity和Task(栈)的关系
Task就像一个容器,而Activity就相当与填充这个容器的东西,第一个东西(Activity)则会处于最下面,最后添加的东西(Activity)则会在最低端。从Task中取出东西(Activity)则是从最顶端取出。
二、界面跳转和服务的启动都会用到Intent,现在介绍Intent Flag是关于Activity的跳转
Intent intent = new Intent(this,xxx.class);
//如果activity在task存在,拿到最顶端,不会启动新的Activity
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
//如果activity在task存在,将Activity之上的所有Activity结束掉
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//默认的跳转类型,将Activity放到一个新的Task中
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//如果Activity已经运行到了Task,再次跳转不会在运行这个Activity
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
写了个小升级版,进入Activity1后立即跳以Activity3
package wzq.android.three;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;