2010-03-23 79 views
0

我試圖動態地創建和刪除選項卡。通常應該爲TabSpec中創建的每個選項卡設置活動。但是如何在動態創建標籤時執行此操作?這裏我使用框架佈局來顯示標籤內容。如果我嘗試通過設置標籤內容來使用相同的活動,則文本會變得重疊。在這裏,我必須從EditText視圖中讀取文本,並將其設置爲選項卡內容,並且每當導航到該選項卡時都應顯示內容。如何爲動態創建的每個選項卡創建活動?

+0

「平時的活動應在則tabspec創建的每個選項卡中設置。」不,通常應該爲在TabSpec中創建的每個選項卡設置一個視圖。 *可以*使用活動作爲標籤內容,但我不推薦它,因爲它增加了沒有價值的開銷。 – CommonsWare 2010-03-23 14:24:34

+0

如何爲所有標籤頁使用相同的活動。我嘗試過,但數據重疊。即使我們想要處理巨大的標籤頁內容,它也會讓我頭腦發熱或麻煩。 – Kantesh 2010-03-24 04:38:07

+0

@Kantesh現在使用片段:http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/ – 2014-02-17 03:25:19

回答

5

試試這個

protected TabHost tabs; 

// ... 

/** 
* Init tabs. 
*/ 
private void initTabs() { 
tabs = (TabHost) findViewById(R.id.tabhost); 
tabs.setup(); 
tabs.setBackgroundResource(R.drawable.bg_midgray); 

TabHost.TabSpec spec; 

// Location info 
txtTabInfo = new TextView(this); 
txtTabInfo.setText("INFO"); 
txtTabInfo.setPadding(0, 0, 0, 0); 
txtTabInfo.setTextSize(14); 
txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_inactive_right_inactive); 
txtTabInfo.setTextColor(Color.DKGRAY); 
txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP); 
txtTabInfo.setHeight(39); 
spec = tabs.newTabSpec("tabInfo"); 
spec.setContent(R.id.tabInfo); 
spec.setIndicator(txtTabInfo); 
tabs.addTab(spec); 

// Maps 
txtTabMap = new TextView(this); 
txtTabMap.setText("MAP"); 
txtTabMap.setTextSize(14); 
txtTabMap.setPadding(0, 0, 0, 0); 
txtTabMap.setBackgroundResource(R.drawable.bg_tab_middle_inactive_right_active); 
txtTabMap.setTextColor(Color.DKGRAY); 
txtTabMap.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP); 
txtTabMap.setHeight(39); 
spec = tabs.newTabSpec("tabMap"); 
spec.setContent(R.id.tabMap); 
spec.setIndicator(txtTabMap); 
tabs.addTab(spec); 

tabs.setCurrentTab(0); 

tabs.setOnTabChangedListener(this); 
} 

// ... 
相關問題