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