2014-04-20 25 views
0

所以我在我的應用程序(4.2.2)中做了一個tabhost,但是當我用標籤的標題填充標籤指示符時圖像消失了,並且當我僅放置圖像路徑時圖像,但我想顯示兩者。誰能幫助我TabHost中的文本和圖像desapear

在這種情況下,圖像會出現,但爲y可以看到標題是不是充滿 公共類MainActivity擴展TabActivity {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    try 
    { 
     Resources res = getResources(); 
     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 
     Intent intent; 
     tabHost.clearAllTabs(); 

     // create an intent for the tab which points at the class file for that tab 
     intent = new Intent().setClass(this, AllProductsActivity.class); 

     //give the tab a name and set the icon for the tab 
     spec = tabHost.newTabSpec("Pesagem").setIndicator("", res.getDrawable(R.drawable.icon_config)).setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, AllContActivity.class); 
     spec = tabHost.newTabSpec("Contagem").setIndicator("", res.getDrawable(R.drawable.icon_config)).setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, AllGuiaActivity.class); 
     spec = tabHost.newTabSpec("Guia").setIndicator("", res.getDrawable(R.drawable.icon_config)).setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(0); 
    } 
    catch(Exception e) 
    { 
     System.out.println(e.toString()); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

如果我這樣做,它僅顯示標題:

 intent = new Intent().setClass(this, AllGuiaActivity.class); 
     spec = tabHost.newTabSpec("Guia").setIndicator("Pesagem", res.getDrawable(R.drawable.icon_config)).setContent(intent); 
     tabHost.addTab(spec); 

我能做些什麼,有標題和圖像選項卡上

回答

2
View view = LayoutInflater.from(context).inflate(R.layout.tabs, null); 
TextView tv = (TextView) view.findViewById(R.id.tabsText); 
tv.setText(text); 
ImageView iv = (ImageView) view.findViewById(R.id.tabsImage); 
iv.setImageResource(image); 

TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(view) 
       .setContent(new Intent(this, activity)); 
mTabHost.addTab(setContent); 

佈局可以只是一個帶有TextView和ImageView的LinearLayout。

+0

你說要創建一個圖像和文本視圖的佈局,然後膨脹它並將其存儲到我想要的tabspec。我有3個選項卡生病只需重複您的示例代碼3次 – user3121133

+0

正確,做三次。 – nasch