Android的颜色选择不与自定义属性工作 [英] Android color selector doesn't work with custom attributes
问题描述
我有在attrs.xml
I've got in attrs.xml
<resources>
<!-- theme specific colors -->
<attr format="reference|color" name="foreground" />
<attr format="reference|color" name="background" />
</resources>
然后在theme.xml
And then in theme.xml
<style name="MyTheme" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="foreground">#0000FF</item>
<item name="background">#00FF00</item>
</style>
我还创建颜色选择器名为forground_to_background.xml
I also created color selector named forground_to_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="?background"/> <!-- pressed -->
<item android:state_focused="true" android:color="?background"/> <!-- focused -->
<item android:color="?foreground"/> <!-- default -->
</selector>
现在我想使用它都聚集在TextView的:
Now I'd like to use it all together in TextView:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/forground_to_background" />
不幸的是它不工作。而不是有漂亮的绿蓝色我有只有一种颜色 - 红色。 TextView的永远是红色的。当我改变的TextView使用?前景颜色会发生变化。此外,当我在颜色选择器改变从?XXXX硬codeD值#00F颜色开始工作。
Unfortunately it doesn't work. Instead of having nice green blue colors I've got only one color - red. TextView is always red. When I change TextView to use "?foreground" color will change. Also when I change in colors selector from "?xxxx" to hardcoded value as "#00f" color start to work.
在哪里的问题?我究竟做错了什么?
Where is problem? What am I doing wrong?
编辑: 我相信这是重复的问题/错误<一的href="http://stackoverflow.com/questions/3757696/can-a-selector-resource-use-a-color-defined-in-a-style">Can一个选择资源使用的样式定义的颜色?
I believe it is duplicate of problem/bug Can a selector resource use a color defined in a style?
EDIT2: 此外,当我尝试使用这个TextView的ListView中应用程序崩溃。它不能解析XML。
Moreover when I try use this TextView in ListView application crashes. It cannot parse XML.
推荐答案
之所以出现这种情况是,我有不同的语境。虽然充气语境意识到我的主题ATTRS,但到ListView适配器我通过ApplicationContext的是不知道这些的attrs的。现在,我不知道为什么它不知道他们。)
The reason why this happens is that I have different Context. While inflating Context is aware of my theme attrs, but to the ListView adapter I passed ApplicationContext that wasn't aware of those attrs. Now I don't know why it doesn't know about them ;)
这篇关于Android的颜色选择不与自定义属性工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!