形状绘制中无法识别颜色状态列表
我定义了以下可绘制 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
:
<?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
中为我的形状定义的颜色有任何变化,而视图状态实际上已更改(selector_png_drawable.xml
是一个 指标)。
但是,当我按以下方式修改 my_background_drawable.xml
时,一切都很好:
<?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
资源在 ColorStateList
中使用时,颜色状态信息是否真的会丢失? code>ShapeDrawable 还是我做错了?
I define following 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>
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>
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).
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>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ColorStateList
不能作为 XML 定义中
的属性传递,也不能作为
的任何属性传递。该属性从 XML 中扩充为 Color 资源,然后传递给 Drawable 的setColor()
方法,该方法仅采用单个 ARGB 值。只有一种类型的 Drawable 实例被设计为包含并基于状态呈现多个项目,那就是 StateListDrawable ,这是当您膨胀< 时所得到的。 /代码>。所有其他 Drawable 实例都只是该集合的成员或独立绘制。
另请注意,膨胀的
项实际上是GradientDrawable
而不是ShapeDrawable
。如果您查看GradientDrawable
的inflate()
方法在源代码中,您可以获得有关如何使用每个属性的所有详细信息。哈!
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'ssetColor()
method, which only takes a single ARGB value.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.Note also that an inflated
<shape>
item is actually aGradientDrawable
and not aShapeDrawable
. If you check out theinflate()
method ofGradientDrawable
in the source, you can get all the detail you could ask for on how each attribute is used.HTH!
事实上,您可以将
ColorStateList
指定为shape
的 xml 内的纯色 ->GradientDrawable
,但这只是一个 新功能棒棒糖。旧版本的
GradientDrawable
仅接受颜色资源。如果您有兴趣,目前正在研究兼容的替代方案。
In fact you can assign a
ColorStateList
as a solid color inside of the xml of a ashape
->GradientDrawable
, but that is only a new feature in Lollipop.Older versions of
GradientDrawable
only accept color resources.Currently working on a compat alternative if you are interested.
您正在这样做......只需将其替换
为
update:
在 my_background_drawable.xml 的程序代码中
You are doing it wring .... just Replace this
with
update:
in your program code in the my_background_drawable.xml