颜色状态列表不是形状绘制对象认可 [英] Color State List not recognized in Shape Drawable
问题描述
我定义下绘制 my_background_drawable.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="@color/color_stateful" />
</shape>
</item>
<item android:drawable="@drawable/selector_png_drawable" />
</layer-list>
和我还定义下列颜色状态列表资源 color_stateful.xml
:
And I also define following color state list resource color_stateful.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#FF00ff00"/>
<item android:color="#FFff0000"/>
</selector>
当我设置的给定 my_background_drawable
作为背景的一些看法,然后我无法观察到在 color_stateful.xml $ C定义颜色的任何变化$ C>我的形状,而视图状态实际上是改变(
selector_png_drawable.xml
是一个指标)。
When I set given my_background_drawable
as a background for some view then I cannot observe any change in color defined in color_stateful.xml
for my shape, while the view state is actually changed (selector_png_drawable.xml
is an indicator).
然而,一切都只是罚款当我修改我的 my_background_drawable.xml
以下列方式:
However everything is just fine when I modify my my_background_drawable.xml
in the following way:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This doesn't work
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="@color/color_stateful" />
</shape>
</item>
-->
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="#FF00ff00" />
</shape>
</item>
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="#FFff0000" />
</shape>
</item>
</selector>
</item>
<item android:drawable="@drawable/selector_png_drawable"" />
</layer-list>
所以,这是真的,当 ColorStateList
资源内部使用的 ShapeDrawable
或者是颜色状态信息只是失去了我做错了?
So is it true that color state information is just lost when ColorStateList
resource is used within a ShapeDrawable
or am I doing it wrong?
推荐答案
A ColorStateList
不能对&LT通过为属性;固体&GT;
在一个XML定义或&LT真的任何属性;形状&GT;
。此属性夸大了XML作为一个颜色资源,然后传递到绘制对象的 setColor()
方法,该方法只需要一个ARGB值。
A ColorStateList
cannot be passed as an attribute for <solid>
in an XML definition, or really any attribute of a <shape>
. This attribute is inflated out of the XML as a Color resource and then passed to the Drawable's setColor()
method, which only takes a single ARGB value.
只有一种类型,它的目的是遏制和基于状态present多个项目绘制对象实例,那就是 StateListDrawable
,这是你会得到什么当你吹一个&LT;选择&GT;
。所有其他绘制对象实例是为了仅仅是这个系列的成员或绘制的独立。
There is only one type of Drawable instance that is designed to contain and present multiple items based on state, and that is StateListDrawable
, which is what you get when you inflate a <selector>
. All other Drawable instances are meant to simply be members of this collection or drawn standalone.
此外请注意膨胀&LT;形状&GT;
产品实际上是 GradientDrawable
,而不是 ShapeDrawable
。如果检查出膨胀()
的方法 GradientDrawable
<一个href="http://$c$csearch.google.com/$c$csearch#uX1GffpyOZk/graphics/java/android/graphics/drawable/GradientDrawable.java">in来源,你可以得到所有你可以要求如何每个属性使用的细节。
Note also that an inflated <shape>
item is actually a GradientDrawable
and not a ShapeDrawable
. If you check out the inflate()
method of GradientDrawable
in the source, you can get all the detail you could ask for on how each attribute is used.
心连心!
这篇关于颜色状态列表不是形状绘制对象认可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!