2012-01-02 23 views
0

我想成立一​​個tabHost和列表中的一個在另一個之下,所以我把它添加到一個垂直線性佈局,但是當我嘗試addView標籤主機通過活動安排上面列表中的tabHost

public TabHost peopleTabHost; 

private void CreateNewTab(String tagName, String displayedName, Class<?> intentClass) 
{ 
    Intent intent = new Intent().setClass(this, intentClass); 
    TabHost.TabSpec spec = peopleTabHost.newTabSpec(tagName).setIndicator(displayedName); 
    spec.setContent(intent); 
    peopleTabHost.addTab(spec);  
} 

public void onCreate(Bundle savedInstanceState) 
{ 
     super.onCreate(savedInstanceState); 
     LayoutParams rootParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
     LinearLayout rootLayout = new LinearLayout(this); 
     rootLayout.setLayoutParams(rootParams); 
     rootLayout.setOrientation(LinearLayout.VERTICAL); 
     LinearLayout.LayoutParams listParams = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT); 
     listParams.weight = 1; 
     ListView lv_chosen = new ListView(this); 
     lv_chosen.setLayoutParams(listParams); 
     lv_chosen.setTextFilterEnabled(true); 

     peopleTabHost = getTabHost(); 
     CreateNewTab("groups", "Groups", GroupsActivity.class); 
     CreateNewTab("everyone", "Everyone", EveryoneActivity.class); 
     CreateNewTab("contacts", "Contacts", ContactsActivity.class); 

     peopleTabHost.setCurrentTabByTag("everyone"); 
     rootLayout.addView(peopleTabHost); 
     rootLayout.addView(lv_chosen); 
} 
崩潰

感謝

回答

0

OnCreate(),你必須先創建TabHost:

// instead of peopleTabHost = getTabHost(); 
peopleTabHost = new TabHost(this); 
setTabHost(peopleTabHost()); 

Furtherthemore你必須創建一個內部佈局TabWidget和合作內容(FrameLayout)。因此繼續

LinearLayout tabhostLinearLayout = new LinearLayout(this); 
tabhostLinearLayout.setOrientation(LinearLayout.VERTICAL); 
peopleTabHost.addView(tabhostLinearLayout); 

TabWidget tabWidget = new TabWidget(this); 
tabhostLinearLayout.addView(tabWidget); 

// the FrameLayout for the content 
FrameLayout frameLayout = new FrameLayout(this); 
tabhostLinearLayout.addView(frameLayout); 

// and finally 
peopleTabHost.setup(); 

,這似乎也是在OnCreate()年底丟失:

setContentView(rootLayout); 

因爲這是一個大量的工作,我建議使用XML佈局這一點。

+0

謝謝,但一開始喜歡你並顯示墜毀時,我試圖以一個新的標籤添加到它,當我把它就像我的代碼顯示它並沒有崩潰了我所做的peopleTabHost。我確實定義了它,如果你的意思是在創建新的選項卡方法之上。另外,方法setTabHost(TabHost)未定義。 – Someonation 2012-01-02 16:11:20

0
+0

什麼是片段標籤? – Someonation 2012-01-02 17:09:33

+0

和很多它的進口量不能得到解決,我的電腦上至少,shuold我下載一個特殊的包 – Someonation 2012-01-02 17:15:45

+0

這只是如何模塊化一個TabHost的標籤的片段的例子。 只需編輯例如成你需要的東西,關鍵是TabHost正確處理這是一件好事,似乎你有麻煩了。 :-) – einschnaehkeee 2012-01-02 17:22:01