对于TabHost中setContent()使用Intent的一点总结

本文介绍了在Android中使用TabHost创建选项卡界面的三种方法:继承TabActivity、Activity及ActivityGroup。详细展示了每种方法的实现代码及对应的XML布局文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建TabHost有两种方式

第一种:继承TabActivity,使用getTabHost初始化。

第二种:继承Activity,使用this.findViewById(R.id.tabhost)初始化。

但是,当我们的setContent()中的参数是Intent时,也就是他的tab包含的是Activity而不是其他的布局或者组件时,之前正常的代码会发生错误,这里我说一下一点总结吧

 

第一,继承Activity 

public class TabDemoActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
                // 注意下面这段
		LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
	        mLocalActivityManager.dispatchCreate(savedInstanceState);
	        tabHost.setup(mLocalActivityManager);
	    
		tabHost.addTab(tabHost.newTabSpec("one").setIndicator("tab1")
				.setContent(new Intent(this, FirstActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("two").setIndicator("tab2")
				.setContent(new Intent(this, SecondActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("three").setIndicator("tab3")
				.setContent(new Intent(this, ThirdlyActivity.class)));
	}
}

第二,继承ActivityGroup

public class TabDemoActivity extends ActivityGroup
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
		// 注意下面这段
                tabHost.setup(this.getLocalActivityManager());
	    
		tabHost.addTab(tabHost.newTabSpec("one").setIndicator("tab1")
				.setContent(new Intent(this, FirstActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("two").setIndicator("tab2")
				.setContent(new Intent(this, SecondActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("three").setIndicator("tab3")
				.setContent(new Intent(this, ThirdlyActivity.class)));
	}
}

为什么需要调用setup方法,官方给出的解释,setup方法主要是初始化了TabConnect和TabWidget

 If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend TabActivity.

上面这两种代码的对应XML,注意除了tabhost的id可以自定义外,其他的必须使用系统的id

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:padding="5dp" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.0" >
        </TabWidget>
    </LinearLayout>

</TabHost>


第三,继承TabActivity

public class TabDemoActivity extends TabActivity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
                // 注意id
		TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
	    
		tabHost.addTab(tabHost.newTabSpec("one").setIndicator("tab1")
				.setContent(new Intent(this, FirstActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("two").setIndicator("tab2")
				.setContent(new Intent(this, SecondActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("three").setIndicator("tab3")
				.setContent(new Intent(this, ThirdlyActivity.class)));
	}
}

第三种使用的XML如下

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:padding="5dp" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.0" >
        </TabWidget>
    </LinearLayout>

</TabHost>


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值