如何更新tabhost android中的特定标签图标?
问题描述:
我有4个选项卡,其中之一是通知图标。默认情况下使用标签规格设置所有图标。如何更新tabhost android中的特定标签图标?
spec = host.newTabSpec("Tab Four");
spec.setContent(R.id.tab5);
spec.setContent(new Intent(getApplicationContext(), ICNotificationActivity.class));
spec.setIndicator("", getResources().getDrawable(R.drawable.tab_notification_noted));
host.addTab(spec);
经过一段时间后,我必须更新标签4图标与另一个资源文件。如何更新tabhost?有没有更新功能可用,如host.addTab(spec)?
答
在这里,我循环所有选项卡,并更改选定选项卡的颜色和图标。 在循环我设置所有选项卡未选中首先比只有选定的选项卡设置为更新的图标和颜色。
private void setTabColor(TabHost tabhost, int position) {
for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
// unselected
tabhost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_unselected);
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(R.id.tvTabTitle);
tv.setTextColor(getResources().getColor(R.color.text_white));
}
if (position > 0) {
// selected
tabhost.getTabWidget().setCurrentTab(position);
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
.setBackgroundResource(R.drawable.tab_selected);
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(tabhost.getCurrentTab()).findViewById(R.id.tvTabTitle);
tv.setTextColor(getResources().getColor(R.color.tab_color));
}
}
可以通过使用
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
//Do something here
setTabColor(myTabHost, myTabPosition);
}
}, 5000);
显示java.lang.NullPointerException调用上述方法:尝试调用虚拟方法 '无效android.view.View.setBackgroundResource(INT)' 上的空对象引用。显示此日志错误。 {host =(TabHost)findViewById(R.id.tabHost); host.setup(); host.getTabWidget()。setStripEnabled(false); host.getTabWidget()。setDividerDrawable(R.drawable.border_tab_activity); host.setup(this.getLocalActivityManager());},已经定义了tabhost。 –
@SARATHV我已经给你我的代码,你需要根据你的需要来改变。 –
代码正常工作,谢谢。我在表格中犯了错误,这就是为什么我得到了空指针异常 –