9

我想设计一个自定义的文本选择器,当用户单击 TextView 时更改文本颜色。但出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.view.InflateException: Binary XML file line #55: Error inflating class

这是我所拥有的:drawable/text_selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_enabled="false" 
        android:state_focused="true" 
        android:drawable="@color/black" /> 
  <item android:state_pressed="true" 
        android:drawable="@color/blue" /> 
  <item android:state_focused="true" 
        android:drawable="@color/black" /> 
</selector> 

布局/textview.xml

<TextView android:id = "@+id/last_page_button"
    android:text="@string/last_page_button_string" 
    android:gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:background="#ffffff"
    android:textColor = "@drawable/text_selector"
    android:layout_weight="1" />

值/color.xml

<resources> 
    <color name="white">#ffffffff</color> 
    <color name="black">#ff000000</color> 
    <color name="blue">#ffccddff</color>

4

2 回答 2

24

您不能将可绘制对象分配给 textColor。它必须是一种颜色。

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"

        android:color="@color/color1" />
    <item
        android:color="@color/color2" />
</selector>

在 res 中创建文件夹颜色,将此文件保存为 mycolor.xml 并将其分配给 textColor 作为 @color/mycolor

于 2010-05-07T21:22:27.810 回答
0

使用选择器如下:

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_selected="true"

    android:color="@color/c1" />
<item
    android:color="@color/c2" />

于 2012-09-21T10:40:31.007 回答