2011-03-01 103 views

回答

21

您可以使用下面的代码

TabHost tabHost = getTabHost(); 
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
     { 
      TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs 
      tv.setTextColor(Color.parseColor("#ffffff")); 
     } 
     TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab 
     tv.setTextColor(Color.parseColor("#000000")) 
+0

ColorStateList是更好的方式 – aelam 2013-08-05 02:12:03

+0

谢谢alot ..它的工作 – jyomin 2014-01-03 04:25:49

34

我使用ColorStateList,发现它更优雅。这里有一个例子:

tab_text.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:color="@color/tab_active" /> 
    <item android:state_selected="false" android:color="@color/tab_inactive" /> 
</selector> 

在你的TextView,只需设置文字颜色为指向该文件与:

android:textColor="@color/tab_text" 
+0

这个解决方案比其他发布的解决方案更清洁 – 2011-10-06 15:14:31

+0

它的工作,这个职位很简单,状态转发和它保存我的时间,谢谢。 – Ravikumar11 2013-03-20 09:36:41