【问题标题】:Change tab text color when selected选择时更改选项卡文本颜色
【发布时间】:2016-04-08 16:39:53
【问题描述】:

我想在选项卡被选中时更改它的文本颜色。

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

有什么建议吗?

【问题讨论】:

    标签: android android-fragments android-tabs pagerslidingtabstrip


    【解决方案1】:

    你可以使用这个代码sn-p

     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
            }
        });
    

    在所有选项卡相关的初始化完成后,将此代码 sn-p 放入您的 onCreate() 方法中。

    【讨论】:

      【解决方案2】:
      tab.setTextColor(tabTextColor);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-12
        • 1970-01-01
        • 2020-09-10
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        相关资源
        最近更新 更多