如何更新 Tab android 中的指标图像?
How to update indicator image in Tab android?
我有两个带有两个指标的选项卡,它工作正常
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());
}
我试过使用上面的代码但是行不通!
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));
我有两个带有两个指标的选项卡,它工作正常
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());
}
我试过使用上面的代码但是行不通!
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));