2013-08-30 27 views
0

我用android.support.v4.app.FragmentTabHost編寫了一個Tab Navigation應用程序。 我想添加圖像背景到我的標籤。 代碼是在這裏:如何使用FragmentTabHost設置選項卡圖像?

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_tab); 
    instance = this; 


    history = new HashMap<String, Stack<Fragment>>(); 
    tabhost = (FragmentTabHost)findViewById(android.R.id.tabhost); 

    tabhost = (FragmentTabHost)findViewById(android.R.id.tabhost); 
    tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 

    TabSpec tab1 = tabhost.newTabSpec("0").setIndicator("Home"); 
    TabSpec tab2 = tabhost.newTabSpec("1").setIndicator("Sign Up"); 
    TabSpec tab3 = tabhost.newTabSpec("2").setIndicator("Tab3"); 



    System.out.print("second out"); 




    tabhost.addTab(tab1, myFragment.class, null); 
    tabhost.addTab(tab2, myFragment2.class, null); 
    tabhost.addTab(tab3, myFragment3.class, null); 
+0

http://stackoverflow.com/questions/4756447/can-we-set-a-background-圖片到標籤看到這個&這http://stackoverflow.com/a/4756506/826657 –

回答

1

可以從資源添加可繪製到TabSpec。這就是:

Resources res = getResources(); // Resource object to get Drawables 
TabHost tabHost = getTabHost(); 
TabHost.TabSpec spec; // Resusable TabSpec for each tab 

// Initialize a TabSpec for each tab and add it to the TabHost 
spec = tabHost.newTabSpec("1").setIndicator(res.getString(R.string.tabname1), 
              res.getDrawable(R.drawable.tabimage1)); 
tabHost.addTab(spec, FragmentOne.class, null); 

spec = tabHost.newTabSpec("2").setIndicator(res.getString(R.string.tabname2), 
              res.getDrawable(R.drawable.tabimage2)); 
tabHost.addTab(spec, FragmentTwo.class, null); 

spec = tabHost.newTabSpec("3").setIndicator(res.getString(R.string.tabname3), 
              res.getDrawable(R.drawable.tabimage3)); 
tabHost.addTab(spec, FragmentThree.class, null); 

//... and so on ... 

在這裏看到完整的教程:

http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/

+0

我認爲這些方法是與傳統的tabwidget不支持android.support.v4.app.fragmenttabhost支持。我嘗試過,但它不工作 – amjad

+0

這實際上應該工作,請參閱我發佈的教程鏈接。他們正在使用支持Fragmenttabhost。 –

+1

耶夥計,我認爲它應該可以工作,但是如果你能看到的話,它不會從drawables中獲得我的圖像! – amjad

相關問題