使用ActivityGroup类显示多个Activity
有些情况下需要把一个Activity分割成几部分,如上半部分绘制图形,下半部分显示图表,这时就需要用到ActivityGroup。
定义一个类继承自ActivityGroup,Java文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.h.callsta;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
public class DetailActivity extends ActivityGroup{
private LinearLayout lo1, lo2;
// LocalActivityManager对象
LocalActivityManager lam = getLocalActivityManager();
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// 获得LinearLayout对象
lo1 = (LinearLayout) findViewById(R.id.lo1);
lo2 = (LinearLayout) findViewById(R.id.lo2);
start_activity();
}
public void start_activity(){
// 加载两个窗口
Window win1 = lam.startActivity( "0" ,
new Intent(DetailActivity. this , DetailChart. class ));
View view1 = win1.getDecorView();
lo1.addView(view1);
Window win2 = lam.startActivity( "1" ,
new Intent(DetailActivity. this , DetailList. class ));
View view2 = win2.getDecorView();
lo2.addView(view2);
}
}
|
在该类对应的XML布局文件中,定义了两个LinearLayout,分别用来显示两个Activity。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@+id/container"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<LinearLayout
android:id= "@+id/lo1"
android:layout_width= "fill_parent"
android:layout_height= "match_parent"
android:layout_weight= "1"
android:orientation= "vertical" >
</LinearLayout>
<LinearLayout
android:id= "@+id/lo2"
android:layout_width= "fill_parent"
android:layout_height= "match_parent"
android:layout_weight= "1"
android:orientation= "vertical" >
</LinearLayout>
</LinearLayout>
|
在Java文件中,通过getLocalActivityManager()获得LocalActivityManager对象,再通过findViewById()方法获得布局文件中的两个LinearLayout。最后,用LocalActivityManager对象的startActivity()方法,分别启动两个Activity--DetailChart(图形)和DetailList(列表),并分别用getDecorView()方法获得View对象,addView()到两个LinearLayout上,这样,就实现了在一个Activity类同时显示两个Activity。
用ActivityGroup解决TabHost中多个Activity跳转问题
一叶飘舟
于 2012-02-26 22:22:16 发布
1569
收藏
分类专栏: Android UI 文章标签: android layout import button encoding class
版权
Android UI
专栏收录该内容
431 篇文章3 订阅
订阅专栏
最近在做一个程序,刚开始没考虑全,就用TabHost做了,后来才发现程序中,需要在一个TabHost内实现多个Activity的跳转,网上搜了一翻,有人建议把TabHost改成Button,然后每个Activity中都处理加入的Button,这样是可以解决问题,但是修改起来很繁琐,所以还是继续寻找替代方法。在网上搜到了《使用ActivityGroup来切换Activity和Layout》一文,但是用在我的程序中还需要有大的改动,所以索性我就自己写了个测试例子,不错,成功了,拿出来和大家分享一下,希望对大家有帮助!
下面图片是测试程序的效果图
两个选项卡的实现
布局文件 main.xml
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">
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="3">
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Java代码实现 MainActivity.java
Java代码
package hkp.test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("First")
.setContent(new Intent(this,FirstGroupTab.class)));//第一个选项卡使用一个ActivityGroup
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("Second")
.setContent(new Intent(this, SecondTab.class)));//第二个选项卡是一个Activity
tabHost.setCurrentTab(0);
}
}
package hkp.test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("First")
.setContent(new Intent(this,FirstGroupTab.class)));//第一个选项卡使用一个ActivityGroup
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("Second")
.setContent(new Intent(this, SecondTab.class)));//第二个选项卡是一个Activity
tabHost.setCurrentTab(0);
}
}
使用 ActivityGroup的管理
Java代码
package hkp.test;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
/**
* @author HuangKaipeng [email protected]
* 2011-10-5
*
*/
public class FirstGroupTab extends ActivityGroup {
/**
* 一个静态的ActivityGroup变量,用于管理本Group中的Activity
*/
public static ActivityGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
group = this;
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// super.onBackPressed();
//把后退事件交给子Activity处理
group.getLocalActivityManager()
.getCurrentActivity().onBackPressed();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//把界面切换放到onResume方法中是因为,从其他选项卡切换回来时,
//调用搞得是onResume方法
//要跳转的界面
Intent intent = new Intent(this, FirstActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = group.getLocalActivityManager().startActivity("FirstActivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
group.setContentView(view);
}
package hkp.test;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
/**
* @author HuangKaipeng [email protected]
* 2011-10-5
*
*/
public class FirstGroupTab extends ActivityGroup {
/**
* 一个静态的ActivityGroup变量,用于管理本Group中的Activity
*/
public static ActivityGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
group = this;
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// super.onBackPressed();
//把后退事件交给子Activity处理
group.getLocalActivityManager()
.getCurrentActivity().onBackPressed();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//把界面切换放到onResume方法中是因为,从其他选项卡切换回来时,
//调用搞得是onResume方法
//要跳转的界面
Intent intent = new Intent(this, FirstActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = group.getLocalActivityManager().startActivity("FirstActivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
group.setContentView(view);
}
ActivityGroup中的第一个Activity
Java代码
package hkp.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author HuangKaipeng [email protected]
* 2011-10-5
*
*/
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
//跳转到第二个界面
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(FirstActivity.this, SecondActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = FirstGroupTab.group.getLocalActivityManager()
.startActivity("SecondActivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
FirstGroupTab.group.setContentView(view);
}
});
}
}
package hkp.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author HuangKaipeng [email protected]
* 2011-10-5
*
*/
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
//跳转到第二个界面
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(FirstActivity.this, SecondActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = FirstGroupTab.group.getLocalActivityManager()
.startActivity("SecondActivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
FirstGroupTab.group.setContentView(view);
}
});
}
}
XML
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="wrap_content"
android:text="这个是ActivityGroup中的第一个界面!"
/>
<Button android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="跳转到本组中的另一个Activity中"/>
</LinearLayout>
GroupInTab.rar (50.1 KB)
————————————————
版权声明:本文为CSDN博主「一叶飘舟」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jdsjlzx/article/details/7296578