2011-11-09 54 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