我没用过楼主的用法,觉得有用,先收藏
转自http://blog.csdn.net/lfyx1357/article/details/7467194
由于项目需要,需要保持底部菜单(一个TabHost)不变,在TabHost内进行Activity之间跳转。在ios中有自带的控件可以实现,不是很麻烦。而在android没有这样可以直接的控件,怎么办呢?这时使用ActivityGroup可以解决这个问题。
实现的主要原理是,将需要进行多个Activity跳转的TabActivity设置为ActivityGroup,用ActivityGroup管理多个Activity之间的跳转。这里用一个简单的例子作为讲解。
MainActivity为主Activity,为一个TabActivity,其中包含FirstGroupTab和SecondGroupTab;FirstGroupTab为第一个标签页的ActivityGroup,用于管理FirstActivity、SecondActivity和ThirdActivity在第一个标签页内之间的跳转;SecondGroupTab为第二个标签页的Activity。项目结构如下图:
运行截图:
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">
- <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="0dp"
- android:layout_weight="3">
- </FrameLayout>
- </LinearLayout>
- </TabHost>
- </LinearLayout>
first.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:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:text="选项卡一中的第一个界面"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- <Button
- android:id="@+id/btn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="跳转至本选项卡内的第二个界面" />
- </LinearLayout>
second.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:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:text="选项卡一中的第二个界面"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- <Button
- android:id="@+id/btn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="跳转至本选项卡内的第三个界面" />
- </LinearLayout>
third.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:gravity="center_horizontal"
- android:text="选项卡一中的第三个界面"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- </LinearLayout>
secondtab.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:gravity="center_horizontal"
- android:text="选项卡二中的界面"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- </LinearLayout>
MainActivity.java代码:
- package com.wenzil.android;
- import android.app.TabActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TabHost;
- public class MainActivity extends TabActivity {
- private TabHost tabHost; //声明TabHost
- private Intent intent; //声明Intent
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tabHost = getTabHost();
- intent = new Intent(this,FirstGroupTab.class);
- tabHost.addTab(
- tabHost.newTabSpec("First")
- .setIndicator("选项卡一")
- .setContent(intent));
- intent = new Intent(this,SecondGroupTab.class);
- tabHost.addTab(
- tabHost.newTabSpec("Second")
- .setIndicator("选项卡二")
- .setContent(intent));
- tabHost.setCurrentTab(0);
- }
- }
FirstGroupTab.java代码:
- <span style="color:#333333;">package com.wenzil.android;
- import android.app.ActivityGroup;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- //第一个标签页显示的Activity,继承ActivityGroup,管理所有子Activity
- public class FirstGroupTab extends ActivityGroup {
- //用于管理本Group中的所有Activity
- public static ActivityGroup group;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- group = this;
- }
- @Override
- public void onBackPressed() {
- //把后退事件交给子Activity处理
- group.getLocalActivityManager().getCurrentActivity().onBackPressed();
- }
- @Override
- protected void onStart() {
- super.onStart();
- //要跳转的Activity
- Intent intent = new Intent(this, FirstActivity.class);
- // 把Activity转换成一个Window,然后转换成View
- Window w = group.getLocalActivityManager().startActivity(
- "FirstActivity", intent);
- View view = w.getDecorView();
- //设置要跳转的Activity显示为本ActivityGroup的内容
- group.setContentView(view);
- }
- }</span>
FirstActivity代码:
- <span style="color:#333333;">package com.wenzil.android;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- 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) {
- //要跳转的Activity
- Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
- // 把Activity转换成一个Window,然后转换成View
- Window w = FirstGroupTab.group.getLocalActivityManager()
- .startActivity("SecondActivity",intent);
- View view = w.getDecorView();
- //设置要跳转的Activity显示为本ActivityGroup的内容
- FirstGroupTab.group.setContentView(view);
- }
- });
- }
- }