2012-11-08 79 views
3
adapter.addTab(getSupportActionBar().newTab().setText("Tab-1"), 
       Tab1.class, null); 
adapter.addTab(getSupportActionBar().newTab().setText("Tab-2"), 
       Tab2.class, null); 
adapter.addTab(getSupportActionBar().newTab().setText("Tab-3"), 
       Tab3.class, null); 

截至目前,每個Tab都將其TextColor設置爲白色。我希望它在未選中時變成灰色,並在選中時變成白色。那麼,如何更改onTabSelectedonTab未選中中的文字顏色。如何更改ActionBarSherlock選項卡文本顏色?

或者我應該與setCustomView的標籤?這裏再次要採取的TEXTSIZE和所有需要照顧

<style name="my_ActionBarTabStyle" parent="@style/Widget.Sherlock.ActionBar.TabView"> 
    <item name="background">@drawable/tab_indicator_ab_wicfy</item> 
    <item name="android:background">@drawable/tab_indicator_ab_wicfy</item> 
    <item name="android:textColor">@color/black</item> 
</style> 

我試圖用

,但它給了我錯誤文字顏色是不是一個有效的屬性

謝謝

回答

15

您不應該從代碼更改文本顏色。改爲使用color state list resource

定義資源的顏色選擇。在res/color/目錄中定義一個xml文件。該文件將包含:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- use when selected --> 
    <item android:state_selected="true" android:color="#fff" /> 

    <!-- otherwise use --> 
    <item android:color="#888" /> 
</selector> 

然後設置一個樣式的文字顏色:

<item name="android:textColor">@color/my_color_selector</item> 

編輯:

你必須設置的文本顏色在風格中的正確位置!將文本顏色設置爲(android:)actionBarTabTextStyle。主題必須包含:

<style name="MyTheme" parent="Theme.Sherlock.Light.DarkActionBar"> 
    ... 
    <!-- define text style for tabs --> 
    <item name="actionBarTabTextStyle">@style/MyTabTextStyle</item> 
    <item name="android:actionBarTabTextStyle">@style/MyTabTextStyle</item> 
    ... 
</style> 

然後設定選項卡上的文本樣式的文本顏色:

<style name="MyTabTextStyle" parent="Widget.Sherlock.ActionBar.TabText" > 
    <item name="android:textColor">@color/my_color_selector</item> 
</style> 
+0

就像在問題1? –

+0

反正不行:( –

+0

你必須把它加到'res/color'中,並把它作爲'@ color/tab_text_indicator'引用。當預期顏色時不能使用drawable – Tomik

相關問題