2011-11-09 51 views
6

我試圖根據其狀態更改TextView樣式。 我styles.xml包含:TextView state_pressed/state_focused/state_selected樣式更改

<style name="text_normal_ops"> 
    <item name="android:gravity">left</item> 
    <item name="android:textColor">@color/text_usual_color</item> 
    <item name="android:textStyle">bold</item> 
</style> 
<style name="text_normal_ops_pressed"> 
    <item name="android:gravity">left</item> 
    <item name="android:textColor">@color/text_pressed</item> 
    <item name="android:textStyle">bold</item> 
</style> 

我的選擇(text_ops.xml)定義爲:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" style="@style/text_normal_ops_pressed" /> 
    <item android:state_focused="true" style="@style/text_normal_ops_pressed" /> 
    <item android:state_selected="true" style="@style/text_normal_ops_pressed" /> 
    <item style="@style/text_normal_ops"/> 
</selector> 

但是,當我將此我的TextView(style="@drawable/text_ops")這是行不通的。 任何提示?
謝謝

+0

你一直在烏爾水庫styles.xml>值文件夾? – Hanry

+0

是的,我有漢尼。 – trenpixster

回答

9

在android中,根據我的知識,只有兩個狀態列表1. Color State List Resource 2. StateListDrawable。如果您在使用它的風格,那麼請重新檢查文檔

請查看以下鏈接瞭解更多信息

  1. http://developer.android.com/guide/topics/resources/color-list-resource.html
  2. http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
+0

謝謝Maneesh,我想那是問題所在。你知道任何解決方法嗎? – trenpixster

1

問題是行style="@drawable/text_ops"這應該是style =「@ style/text_ops」。

我還沒有嘗試過使用樣式選擇器,但它會很酷,如果它的工作方式。

+0

是的,我知道你的意思。我也認爲將__styles__選擇器文件放在__drawables__文件夾中有點奇怪,但我認爲可以在「values/styles.xml」中包含選擇器。 – trenpixster