Setting Drawable as text color raise an exception 'Color value must start with #'

岁酱吖の 提交于 2019-12-07 02:06:07

问题


In a layout I use a button which I set its textColor to a drawable as follows :

@drawble/text_color_drawable :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- disabled state -->
    <item android:state_enabled="false" android:color="@color/disabled_text_color" /> 
    <item android:color="@color/main_text_color"/>
</selector>

@color/main_text_color :

<color name="main_text_color">#9797A3</color>

But when I use this drawable called text_color_drawable as textColor : android:textColor="@drawable/text_color_drawable"
I get an exception : Exception raised during rendering: Color value text_color_drawable must start with #

Am I doing something wrong ?

Thank you


回答1:


The problem may be@drawble/text_color_drawable: it shouldn't be a 'drawable', but rather a
'color'. Basically what you currently have is a StateListDrawable, but what you really want is a ColorStateList. Both are quite similar, but live in different places in the resources.

That being said, try moving the file from res/drawable to res/color. When you then assign the resource as text color, it should say: android:textColor="@color/text_color_drawable"




回答2:


In my case a refresh button has helped. It is placed in a small toolbar over a design view, near magnifiers (with icons like 1:1, +, -).



来源:https://stackoverflow.com/questions/21971573/setting-drawable-as-text-color-raise-an-exception-color-value-must-start-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!