选择TabHost指示器时,设置文本颜色

时间:2017-03-15 23:35:16

标签: android android-tabhost indicator

选择选项卡时,如何更改指示器的文本颜色。我希望两个标签之间有区别。 感谢

TabHost

I want it like this

2 个答案:

答案 0 :(得分:0)

试试这个,

public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
    {
        View view = mTabHost.getTabWidget().getChildTabViewAt(i);
        if ( view != null ) {

            // get title text view
            TextView textView = (TextView)view. findViewById(R.id.tab_title);
            textView.setTextColor(Color.WHITE);
        }

    }
    title.setTextColor(Color.RED); 
}

答案 1 :(得分:0)

onCreate()方法中查看此documentation,以定义所选和未选中标签的初始背景颜色。

for(int i = 0; i < tabHost.getTabWidget().getChildCount();  i++) {
       tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#8A4117"));
}
   tabHost.getTabWidget().setCurrentTab(1);
   tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#C35817"));

OnTabChangeListener实施为当前活动,然后覆盖onTabChanged()方法。在该方法中,编写以下代码以定义所选选项卡和未选定选项卡的颜色。

@Override
public void onTabChanged(String tabId) {
    for(int i = 0; i < tabHost.getTabWidget().getChildCount(); i++){
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#8A4117"));
    }

    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#C35817"));
}

<强>更新

更改标签文字颜色:

  • 通过xml文件:

    <android.support.design.widget.TabLayout
        app:tabTextColor="@color/your_color"
        app:tabSelectedTextColor="@color/your_color"/>
    
  • 通过代码:

    tabLayout.setTabTextColors(
        ContextCompat.getColor(context, R.color.your_unselected_tab_text_color),
        ContextCompat.getColor(context, R.color.your_selected_tab_text_color)
    );