选中时更改选项卡文本颜色

时间:2016-04-08 16:39:53

标签: android android-fragments android-tabs pagerslidingtabstrip

我想在选中时更改标签文字颜色。

tab example

我尝试使用选择器XML,但什么也没发生。我正在使用此库:https://github.com/astuetz/PagerSlidingTabStrip

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您可以使用此代码段

 prevTab = tabHost.getCurrentTab();// Keep track of the default tab
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){ //tabhost is a variable of type TabHost, which will contain all your tabs
        @Override
        public void onTabChanged(String id) {
            int tab = tabHost.getCurrentTab();
            TextView tv = (TextView) getTabWidget().getChildAt(tab).findViewById(android.R.id.title);
            tv.setTextColor(Color.BLUE);//Set selected tab colour to something you want

            if(prevTab!=-1){// If there was a previously selected tab, set it back to a default colour as it is now unselected
                TextView tv1 = (TextView) getTabWidget().getChildAt(prevTab).findViewById(android.R.id.title);
                tv1.setTextColor(Color.BLACK);
            }
            prevTab = tab; //Update this newly selected tab to the currently selected tab, for same logic to repeat for future tab changes
        }
    });

在完成所有与标签相关的初始化后,将此代码段放在 onCreate()方法中。

答案 1 :(得分:-1)

tab.setTextColor(tabTextColor);