0

现在在我的应用程序中,我有一个TabActivity带有三个固定选项卡的选项卡,每个选项卡都包含一个标准Activity.

这是相关的代码片段:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tab_activity);

    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, Tab1Activity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("tab1").setIndicator("Tab 1", res.getDrawable(R.drawable.Tab1Icon)).setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, Tab2Activity.class);
    spec = tabHost.newTabSpec("tab2").setIndicator("Tab 2", res.getDrawable(R.drawable.Tab2Icon)).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Tab3Activity.class);
    spec = tabHost.newTabSpec("tab3").setIndicator("Tab 3", res.getDrawable(R.drawable.Tab3Icon)).setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

这样,每次我选择一个选项卡时,应用程序都会在屏幕上显示Activity与该选项卡相关联的内容。

我想使用ActionBar设计模式来获得相同的结果。

现在我只对固定选项卡布局感兴趣,如您在此处看到的:

在此处输入图像描述

我应该如何更改我的代码?

这些 SO 问题并没有太大帮助:

4

1 回答 1

2

这样,每次我选择一个选项卡时,应用程序都会在屏幕上显示与该选项卡关联的活动。

这种方法已正式弃用。

我想使用 ActionBar 设计模式来获得相同的结果。我应该如何更改我的代码?

最直接的方法是将那些嵌套的活动转换为片段,然后FragmentTransaction随着所选选项卡的变化而提交。如果您使用的是本机 API 级别 11 操作栏,您可以使用本机 API 级别 11Fragment类并FragmentTransaction传入您的TabListener. 如果您想使用 ActionBarSherlock,您将最终使用 Android 支持包中的Fragmentand FragmentTransaction

于 2012-08-07T12:38:54.870 回答