简体   繁体   English

在标签中更改TextView的文本颜色

[英]Change text colour of TextView in Tab

In my Android application I have a TabHost which contains multiple tabs. 在我的Android应用程序中,我有一个TabHost ,其中包含多个选项卡。 To create each tab, I call a function called createTabView which looks like this: 要创建每个选项卡,我调用一个名为createTabView的函数,如下所示:

private static View createTabView(final Context context, final String text, int layoutId) {
    View view = LayoutInflater.from(context).inflate(layoutId, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    if (tv != null) {
        tv.setText(text.toUpperCase(Locale.getDefault()));
        if(text.toLowerCase().equals("special")){
            tv.setTextColor(R.color.gold);
        }
    }

    return view;
}

The above basically adds the appropriate text to the TextView and colours the TextView to gold if the string is "special". 以上基本上并将相应的文本到TextView和颜色TextView黄金如果字符串为“特殊”。

The problem however is that it doesn't render gold as it should. 但是,问题在于它不能像应有的那样渲染黄金。 It renders a very dark blue colour. 它呈现非常深的蓝色。

I can confirm that R.color.gold is definitely gold. 我可以确认R.color.gold绝对是黄金。 I have used it in many places. 我已经在很多地方使用过它。 I have also tried system colours too but they do not work either. 我也尝试过系统颜色,但是它们也不起作用。 It always shows as a dark blue colour. 它始终显示为深蓝色。

Any ideas? 有任何想法吗?

采用-

textView.setTextColor(getResources().getColor(R.color.gold));

Use this code 使用此代码

  for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
          tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#colorcode"));
        TextView tv = (TextView)   tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
        tv.setTextColor(Color.parseColor("#colorcode"));    

    }
    tabHost.getTabWidget().setCurrentTab(0);
      tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#colorcode"));
    TextView tv = (TextView)   tabHost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
    tv.setTextColor(Color.parseColor("#colorcode"));      
}

and this method 和这种方法

  @Override
   public void onTabChanged(String arg0) {

    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#colorcode"));
        TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
        tv.setTextColor(Color.parseColor("#colorcode"));
    }

    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#colorcode"));
    TextView tv = (TextView) tabHost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
    tv.setTextColor(Color.parseColor("#colorcode"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM