2013-12-12 132 views
1

我想添加Tabhost的背景图像,我已经添加到选择器xml中。但不知道如何在启动Tab时添加资源。下面是标签:如何将图像添加到Tabhost

代码添加: getResources()getDrawable(R.drawable.tabicon)

/* Tabs */ 
     Bundle bundle = getIntent().getExtras(); 
     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 
     Intent intent; 

     // First Activity 
     intent = new Intent().setClass(this, InfoListView.class); 
     spec = tabHost.newTabSpec("some_things").setIndicator("Info").setContent(intent); 
     tabHost.addTab(spec); 

     // Second Activity 
     intent = new Intent().setClass(this, LogListView.class); 
     spec = tabHost.newTabSpec("top_things").setIndicator("Sync Log").setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 95; 
     tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 95; 
     /* Tabs ends */ 
+0

我已经更新了我的答案,它会工作 –

回答

0

创建TextView,设置文本到TextView的,设置背景绘制它,并设置这个TextView的作为indicator

+0

的例子?我如何在TabHost中创建TextView? – user45678

+0

查看下一个答案。 :) –

0

有点像这样:

TextView mTv = new TextView(mContext); 
mTv.setText(/*Your-text*/); 
mTv.setBackgroundDrawable(mContext.getResources.getDrawable(/*id-of-your-image*/)); 
spec = tabHost.newTabSpec("top_things").setIndicator(mTv).setContent(intent); 
tabHost.addTab(spec); 
+0

我明白了......谢谢,会尝试 – user45678

+0

如果它有帮助,你可以接受和upvote它。 :) –

+0

没有必要创建textview只是添加字符串到tabhost –

0

您tabhost后intilize指示灯显示,其他东西添加背景像这样

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabicon); //fro first tab 
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabicon); //for second tab   
+0

Shakeeb ...我把截图如何来。这是不正确的。看起来像我们正在设置图标而不是背景图像......这是正确的方法吗?请看我的问题的截图 – user45678