2

我試圖改變TabWidget文本顏色,都沒有成功,即使我已經嘗試了不同的方式來改變它(見下面的代碼)。文本顏色沒有變化TabWidget

我的背景標籤是圖片:

for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) { 
    tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT); 
} 

我不知道這是否與我現在想做的事情產生某種衝突。

解決方法1:

main.xml中

.... 
    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/tabbarbackground" 
     android:tabStripEnabled="false"    
     style="@style/TabText" 
     /> .... 

style.xml

... <style name="TabText"> 
    <item name="android:textColor">@color/tab_text_color</item> </style> .... 

tab_text_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:textColor="#2daed9" /> 
    <item android:state_selected="false" android:color="#FFFFFF" /> 
</selector> 

解決方案2

for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) { 
    tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);   
    RelativeLayout rl = (RelativeLayout) tabHost.getTabWidget().getChildAt(i); 
    TextView textView = (TextView) rl.getChildAt(1); 
    textView.setTextColor(R.color.tab_text_color); 
} 

tab_text_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:textColor="#2daed9" /> 
    <item android:state_selected="false" android:color="#FFFFFF" /> </selector> 

但既不解決方案工作。

但是,如果我的第二個解決方案

textView.setTextColor (R.color.tab_text_color); 

改變

textView.setTextColor (Color.parseColor ("# ....")); 

它的工作原理,但這種解決方案並不改變文本的顏色,當我點擊它。

謝謝。

+0

在這裏看到的是如何改變文字顏色更好的解決方案。 http://stackoverflow.com/questions/9982182/using-selector-to-change-textview-text-color/15498013#15498013! –

回答

6

我能解決,解決的辦法是不優雅,但工程。我希望誰是爲別人有用的:

首先,我必須設置初始化顏色所有選項卡的TextView的:

for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) { 
    vg = (ViewGroup) getTabHost().getTabWidget().getChildAt(i); 
    tv = (TextView) vg.getChildAt(1); 
    tv.setTypeface(font); 
    if (i == 0) { 
     tv.setTextColor(Color.parseColor("#2daed9")); 
     Currentab = 0; 
    } else { 
     tv.setTextColor(R.color.GrisOscuro); 
    } 
} 

然後,我在重寫方法設置ontabchanged,改變顏色爲每個標籤。脈衝標籤是我(getTabHost()。getCurrentTab())。我按下的最後一個選項卡是Currentab。

getTabHost().setOnTabChangedListener(new OnTabChangeListener() { 
    public void onTabChanged(String tabId) { 
     int i = getTabHost().getCurrentTab(); 
     if (Currentab != i) { 
      vg = (ViewGroup) getTabHost().getTabWidget() 
        .getChildAt(Currentab); 
      tv = (TextView) vg.getChildAt(1); 
      tv.setTextColor(R.color.GrisOscuro); 

      Currentab = i; 
      vg = (ViewGroup) getTabHost().getTabWidget() 
        .getChildAt(i); 
      tv = (TextView) vg.getChildAt(1); 
      tv.setTextColor(Color.parseColor("#2daed9")); 
     } 
    } 
}); 

對不起,我希望對某人有用=)再見!; d

1

嘗試寫這梅索德:

public void onTabChanged(String tabId) { 

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
{ 
TextView tv = (TextView) tabhost.getTabWidget() 
.getChildAt(i).findViewById(R.id.your_text_id); 
    tv.setTextColor(#FFFFFF); 

}

TextView tv = (TextView) tabHost.getTabWidget(). 
getChildAt(tabHost.getCurrentTab()).findViewById(R.id.your_text_id); 

tv.setTextColor(#2daed9); 
}  
+0

謝謝,但不會爲我工作,因爲我使用tabwidget沒有textview和imageview上的XML ...我嘗試做類似的事情,但我不能,因爲我不能訪問具體的選項卡,只有當我做了一個: 對於(int i = 0; i RomRuben

4

在你解決方案2

TabWidget tabwidget=mTabHost.getTabWidget(); 
for(int i=0;i<tabwidget.getChildCount();i++){ 
    TextView tv=(TextView) tabwidget.getChildAt(i).findViewById(android.R.id.title); 
    tv.setTextColor(this.getResources().getColorStateList(R.color.tab_text_xml)); 
} 
+0

不錯,效果很好! – berserk