如何在TabHost中更改TabSpec的标题/内容?

时间:2012-01-26 09:48:12

标签: java android

我一直在开发使用以下代码制作TabHost的应用程序:

TabHost.TabSpec spec=mTabHostCategories.newTabSpec("Main");    
spec.setIndicator("Main");
spec.setContent(R.id.listViewMain);
mTabHostCategories.addTab(spec);

mTabSpecFirst=mTabHostCategories.newTabSpec("First");
mTabSpecFirst.setContent(R.id.listViewFirst); 
mTabSpecFirst.setIndicator(mCategoryFirst);
mTabHostCategories.addTab(mTabSpecFirst);

mTabSpecSecond=mTabHostCategories.newTabSpec("Second");
mTabSpecSecond.setContent(R.id.listViewSecond);
mTabSpecSecond.setIndicator(mCategorySecond);
mTabHostCategories.addTab(mTabSpecSecond);    

mTabHostCategories.setCurrentTab(0);

但是我需要更改TabSpecs的标题(指标)和内容。我该怎么做?谢谢。

2 个答案:

答案 0 :(得分:0)

您需要更改mCategoryFirst中的标题 或简单地将带有所需标题的新TextView传递给TabSpec对象

答案 1 :(得分:0)

Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Resusable TabSpec for each tab
    Intent intent; // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, TodaysTakeDemoActivity.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("todaystake").setIndicator("Todays Take",
            res.getDrawable(R.drawable.icontodaystake)).setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, WhatsCasting.class);
    spec = tabHost.newTabSpec("whatscasting").setIndicator(
            "What's Casting", res.getDrawable(R.drawable.iconwhatscasting))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Contacts.class);
    spec = tabHost.newTabSpec("contacts").setIndicator("Contacts",
            res.getDrawable(R.drawable.iconcontact)).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TopListActivity.class);
    spec = tabHost.newTabSpec("actortools").setIndicator("Actor Tools",
            res.getDrawable(R.drawable.icontop10)).setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);