0

我使用 android.support.v4.app.FragmentTabHost 编写了 Tab Navigation 的应用程序。我想将图像背景添加到我的选项卡。代码在这里:

  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);
4

1 回答 1

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/

于 2013-08-30T09:48:13.813 回答