Android 如何在drawable中引用颜色属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10994091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reference colour attribute in drawable?
提问by Michal
I would like to do a simple thing: Define a drawable which has exacly same background colour as system state-pressed background colour. I do it like this in res/drawables/my_drawable.xml:
我想做一件简单的事情:定义一个可绘制的,它的背景颜色与系统状态按下的背景颜色完全相同。我在 res/drawables/my_drawable.xml 中这样做:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true">
<color android:color="?android:attr/colorPressedHighlight"/>
</item>
<item android:state_selected="false">
<color android:color="@color/section_list_background"/>
</item>
</selector>
I always get:
我总是得到:
java.lang.UnsupportedOperationException: Cant convert to color: type=0x2
Any clues?
有什么线索吗?
Regards
问候
回答by petrsyn
You might need to do the following to fix your problem:
您可能需要执行以下操作来解决您的问题:
1) Define 2 colors for each theme in your colors file:
1) 为颜色文件中的每个主题定义 2 种颜色:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="my_color_dark">#ff33B5E5</color>
<color name="my_color_light">#ff355689</color>
</resources>
2) Create file res/values/attrs.xml with contents:
2) 使用内容创建文件 res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="my_color" format="reference" />
</resources>
3) Assuming you have 2 themes in your styles.xml (Theme.dark
and Theme.light
) define:
3)假设你的styles.xml(Theme.dark
和Theme.light
)中有2个主题定义:
<style name="Theme.dark" parent="@style/Theme.Sherlock">
<item name="my_color">@color/my_color_dark</item>
</style>
<style name="Theme.light" parent="@style/Theme.Sherlock.Light">
<item name="my_color">@color/my_color_light</item>
</style>
4) Use the color in a drawable:
4)在drawable中使用颜色:
<color android:color="?attr/my_color"/>
Hope this should fix your problem.
希望这应该可以解决您的问题。
回答by Sachin Gurnani
Just create a new color.xml file in /res/values/color.xml
and put code like the following in it:
只需在其中创建一个新的 color.xml 文件/res/values/color.xml
并将如下代码放入其中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="editcolor">#ff99ffff</color>
<color name="transparent">#00000000</color>
</resources>
回答by XGouchet
You're trying to convert an attribute into a color. Attributes are properties usually attached to a view, which can then be styled using a theme.
您正在尝试将属性转换为颜色。属性是通常附加到视图的属性,然后可以使用主题设置样式。
You need to reference a color resource in your xml. You can do so by creating your own resource :
您需要在 xml 中引用颜色资源。您可以通过创建自己的资源来实现:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPressedHighlight">#FF8800</color>
</resources>
Then reference it like this :
然后像这样引用它:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true">
<color android:color="@color/colorPressedHighlight"/>
</item>
<item android:state_selected="false">
<color android:color="@color/section_list_background"/>
</item>
</selector>
Or you can reference a color available in the Android resources :
或者您可以参考 Android 资源中可用的颜色:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true">
<color android:color="@android:color/holo_orange_light"/>
</item>
<item android:state_selected="false">
<color android:color="@android:color/holo_blue_dark"/>
</item>
</selector>
回答by Alexander Blinov
you can't use ?attr
in xml drawable resources because drawable resources created by aapt in compile time. Attr resources used for dynamic connection in runtime
您不能?attr
在 xml 可绘制资源中使用,因为 aapt 在编译时创建的可绘制资源。用于运行时动态连接的 Attr 资源
回答by Dheeresh Singh
try this way put color in android:drawable as below
尝试这种方式将颜色放入 android:drawable 中,如下所示
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="?android:attr/colorPressedHighlight"/> <!-- pressed -->
<item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
<item android:drawable="@color/section_list_background"/> <!-- default -->
</selector>
or
或者
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="?android:attr/colorPressedHighlight"/>
<item android:state_enabled="false" android:color="@color/section_list_background" />
<item android:color="@color/testcolor5"/>
</selector>