TabHost中的文本和图像消失

时间:2014-04-20 22:45:34

标签: android android-tabhost

所以我在我的应用程序中创建了一个tabhost(4.2.2),但是当我用标签的标题填充选项卡指示符时图像消失,当我只放置图像路径时它会显示图像但是我想要显示两者。任何人都可以帮助我

在这种情况下,图像会出现,但是可以看到标题未填充      公共类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);

如何在标签页上显示标题和图像

1 个答案:

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