如何在使用FragmentTabHost时设置标签图像?

时间:2013-08-30 09:37:26

标签: android background-image fragment-tab-host

我使用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);

1 个答案:

答案 0 :(得分: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/