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设计模式来获得相同的结果。

现在我在具有固定标签布局只是有兴趣,你可以看到here

enter image description here

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

这些所谓的问题没有太大帮助:

+0

您是否使用API​​ 14作为最低要求? – 2012-08-07 11:44:15

+0

@Se_bastiaan - 在我目前的版本中没有,但是我想写一个支持API 14的新版本。 – Matteo 2012-08-07 11:52:57

回答

2

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

此方法已正式废弃。

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

最直接的方法是将这些嵌套活动转换为片段,然后在选定选项卡更改时提交FragmentTransaction s。如果您使用本机API级别11操作栏,则可以使用本机API级别11 Fragment级别和FragmentTransaction传入您的TabListener。如果您想使用ActionBarSherlock,您将使用Android支持包中的FragmentFragmentTransaction结束。

+0

请您提供一些代码片段,以便更好地理解我应该编写的代码结构?thks提前! – Matteo 2012-08-07 18:24:33

+1

@Matteo:http://developer.android.com/guide/topics/ui/actionbar.html#Tabs – CommonsWare 2012-08-07 20:33:21