今天明白了Tabhost嵌套和Tab里实现多个Activity的跳转原理,自己做了一个demo,先看看效果图:
源码 如下:
- (1) DoubleTabHost
- package yy.android.tab;
- import android.app.TabActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TabHost;
- public class DoubleTabHost extends TabActivity {
- /* 注意:
- * 对于TabHost、布局文件中必须包含
- * TabHost、TabWidget 、FrameLayout
- * 如果继承TabActivity,并且通过getTabHost()方法来获取TabHost
- * 那么三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent
- * 如果继承Activity,可以通过findViewById来获取这三个组件,此时ID可自定义
- */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //获得TabHost
- TabHost mTabHost = getTabHost();
- //新建一个tab并设置它的,Tag,标题,图标,内容
- mTabHost.addTab(mTabHost.newTabSpec("YouTube").setIndicator(
- "YouTube",
- getResources().getDrawable(android.R.drawable.arrow_down_float)).setContent(
- new Intent(this, SubTab.class)));
- mTabHost.addTab(mTabHost.newTabSpec("Chrome").setIndicator(
- "Chrome",
- getResources().getDrawable(android.R.drawable.arrow_down_float)).setContent(
- new Intent(this, YActivityGroup.class)));
- mTabHost.setCurrentTab(0);//设置初始选中状态为第一个tab
- }
- }
- //
- (2)YTabDActivity
- package yy.android.tab;
- 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 YTabDActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.normal);
- Button btn = (Button) findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(YTabDActivity.this, SecondActivity.class).
- addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- //把一个Activity转换成一个View
- Window w = YActivityGroup.group.getLocalActivityManager()
- .startActivity("SecondActivity",intent);
- View view = w.getDecorView();
- //把View添加大ActivityGroup中
- YActivityGroup.