使用FragmentTabHost時遇到選中某一個Tab后字體顏色不改變的問題,可以使用以下方法來解決:
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
? ? public void onTabChanged(String s) {
mTabHost.setCurrentTabByTag(tabId);?
? upDateTab(mTabHost);?
});
/**
* 更新文字顏色。
*
* @param mTabHost
*/
private void upDateTab(FragmentTabHost mTabHost) {
for (int i =0; i < mTabHost.getTabWidget().getChildCount(); i++) {
TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(R.id.tv_tab_text_view);
? ? ? ? if (mTabHost.getCurrentTab() == i) {//選中
? ? ? ? ? ? tv.setTextColor(this.getResources().getColor(R.color.tab_select_text_color));
? ? ? ? }else {//不選中
? ? ? ? ? ? tv.setTextColor(this.getResources().getColor(R.color.tab_un_select_text_color));
? ? ? ? }
}
}
使用上面的方法有一個問題就是剛進(jìn)入首頁,文字顏色是沒有改變的,可以在
mTabHost.setCurrentTab(2);
之后去調(diào)用一下update方法即可。