我有两个标签,有两个指标,工作正常
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
peopleTabSpec = tabHost.newTabSpec("people").setIndicator(null, getResources().getDrawable(R.drawable.group_icon));
chatTabSpec = tabHost.newTabSpec("chat").setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green));
tabHost.addTab(peopleTabSpec, MyTestFragment.class, null);
tabHost.addTab(chatTabSpec, MyTestFragment2.class, null);
} else {
peopleTabSpec = tabHost.newTabSpec("people").setIndicator(null, getResources().getDrawable(R.drawable.group_icon, null));
chatTabSpec = tabHost.newTabSpec("chat").setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green, null));
tabHost.addTab(peopleTabSpec, MyTestFragment.class, null);
tabHost.addTab(chatTabSpec, MyTestFragment2.class, null);
}
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_bg_selected);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab_bg_unselected);
现在我需要更改标签更改上的指示器我尝试使用方法再次设置指示器但是它不会工作任何人帮助我
@Override
public void onTabChanged(String s) {
if (s.equals("people")) {
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_bg_selected);
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_bg_unselected);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.group_icon));
chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green));
} else {
peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.group_icon, null));
chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_green, null));
}
} else if (s.equals("chat")) {
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_bg_selected);
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_bg_unselected);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_group));
chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat));
} else {
peopleTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_group, null));
chatTabSpec.setIndicator(null, getResources().getDrawable(R.drawable.icon_chat, null));
}
}
tabHost.getTabWidget().getChildAt(0).setTag(peopleTabSpec.getTag());
tabHost.getTabWidget().getChildAt(1).setTag(chatTabSpec.getTag());
}
我尝试使用上面的代码但不会工作!
答案 0 :(得分:0)
ImageView peopleIndicator = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
peopleIndicator.setImageDrawable(getResources().getDrawable(R.drawable.group_icon, null));
ImageView chatIndicator = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
chatIndicator.setImageDrawable(getResources().getDrawable(R.drawable.icon_chat_green, null));