2011-11-25 23 views
0

我一直在做我自己的自定義選項卡,並且我想根據是否選中來更改選項卡的textColor。爲什麼我不能將不同的textColor應用於製表符?

我創建了 「價值觀」 我自己的風格,這是一個只改變文字顏色:

<style name="TabTextStyle" parent="@android:attr/tabWidgetStyle"> 
    <item name="android:textColor">@drawable/tab_text_color</item> 
</style> 

...基礎上繪製的是認爲 「state_selected」:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:textColor="#000000" 
     android:state_selected="true"/> 
    <item   
     android:textColor="#FFFFFF"/> 
</selector> 

然後,我在樣式我TabIndicator生活在佈局TextView的:

<TextView android:id="@+id/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    style="@style/TabTextStyle"/> 

這使我的三重o crash:\

  • 如果我將TabTextStyle設置爲固定顏色,事情就沒有問題。
  • 如果我將樣式設置爲?@android:attr/tabWidgetStyle ...事情工作正常,但顏色沒有按照我的要求定義。

有沒有人有任何想法?

而且,我試圖設置選擇,這裏我選擇基於state_selected的圖標內的顏色。這會導致圖標消失。

我只能假設,我錯過了一些東西,而且我會對此都錯了,但它似乎是作爲教程說它應該做的事。

希望有人能幫助:)謝謝!

回答

1

嘗試做以下

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- When selected, use grey --> 
<item android:drawable="@color/black" 
     android:state_selected="true" /> 
<!-- When not selected, use white--> 
<item android:drawable="@color/white" /> 
</selector> 

定義自己的顏色資源,或使用Android提供的資源 - 「@android:彩色/黑白」 等

+0

哎呀!現在看起來非常明顯(我認爲該項目可能必須是可繪製的) - 我希望我有遠程訪問權限來嘗試此操作。將在週一的第一件事情,並讓你知道。謝謝。 – sonnyd

0

它的作品!問題是我需要選擇器是一個ColorSelector - 不使用textColors或drawables。

<?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/tabTextSelected"/> 
    <item   
     android:color="@color/tabTextUnselected"/> 
</selector> 
相關問題