2012-04-26 36 views
2

我有如何更改TabHost編程/不乾膠標籤動態

this.tabHost = getTabHost(); 

    // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    // Create an Intent to launch the first Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, FirstGroup.class); 

    // Initialize a TabSpec for the first tab and add it to the TabHost 
    spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar", 
      getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon 
        .setContent(intent); 
    tabHost.addTab(spec1); 

創建tabhost我想以編程方式更改tabhost的標籤:「Regionlar」到「newMenuTabbar」。我找不到任何例子。感謝您的關注。

編輯: 我想從 「Mənzərələr」=> 「secondTabitem」

意圖=新意圖()更改第二的TabItem的標籤setClass(這一點,FirstGroup.class)。

// Initialize a TabSpec for the first tab and add it to the TabHost 
    spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar", 
      getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon 
        .setContent(intent); 
    tabHost.addTab(spec1); 

     // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, SecondActivityGroup.class); 
    spec2 = tabHost.newTabSpec("SecondActivityGroup").setIndicator("Mənzərələr", 
      getResources().getDrawable(R.drawable.img_gallery_icon)) // Replace null with R.drawable.your_icon to set tab icon 
        .setContent(intent); 
    tabHost.addTab(spec2); 

回答

5

試試這個:

final TextView label = (TextView) tabHost.getTabWidget().findViewById(android.R.id.title); 
label .setText(YOUR NEW LABEL); 

希望這將有助於。

+1

如何更改第二個tabitem的標籤?我編輯了這個問題。 – Coenni 2012-04-26 08:45:35

+2

我已經解決了: final TextView label =(TextView)tabHost.getTabWidget()。getChildAt(1).findViewById(android.R.id.title); \t label .setText(「newTabmenu」); 您可以使用getchildat(i)方法更改tabitem的每個標籤。 – Coenni 2012-04-26 08:51:11

+0

是的,你可以得到每個標籤的索引: final int k = tabHost.getCurrentTab(); – 2012-04-26 08:52:21

0

試試這個代碼

public class SlideMainActivity extends TabActivity { 
    public static RelativeLayout headerLayout; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     setContentView(R.layout.main_silde_tab); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
       R.layout.hd_header); 
     setTabs(); 
    } 

    private void setTabs() { 
     addTab("FirstGroup", R.drawable.tab_home, FirstGroup.class); 
     addTab("Regionlar", R.drawable.tab_search, Regionlar.class); 

    } 

    private void addTab(String labelId, int drawableId, Class<?> c) { 
     TabHost tabHost = getTabHost(); 
     Intent intent = new Intent(this, c); 
     TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

     View tabIndicator = LayoutInflater.from(this).inflate(
       R.layout.tab_indicator, getTabWidget(), false); 
     TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
     title.setText(labelId); 
     ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
     icon.setImageResource(drawableId); 

     spec.setIndicator(tabIndicator); 
     spec.setContent(intent); 
     tabHost.addTab(spec); 
    } 

} 
0

b.i的代碼只能改變第一個選項卡的標題。 我認爲這是一種更直接的方式來完成的事情:

((TextView) mTabHost.getTabWidget().getChildTabViewAt(position) 
.findViewById(android.R.id.title)) 
.setText(yourTitle); 

其中position是標籤的位置和yourTitle是你希望的選項卡設置標題。如果你想改變文本當前選項卡,然後,而不是由getCurrentTab更換position,你可以通過getCurrentTabView()

誰寫這應該定義一個setTabText(int position, String text)方法代替getTabWidget().getChildTabViewAt(position) ,不然誰知道他們有一個文本查看id'ed android.R.id.title?或者如果他們已經有了,請賜教。