形状绘制中无法识别颜色状态列表

发布于 2024-12-16 20:12:25 字数 2371 浏览 1 评论 0原文

我定义了以下可绘制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星軌x 2024-12-23 20:12:25

ColorStateList 不能作为 XML 定义中 的属性传递,也不能作为 的任何属性传递。该属性从 XML 中扩充为 Color 资源,然后传递给 Drawable 的 setColor() 方法,该方法仅采用单个 ARGB 值。

只有一种类型的 Drawable 实例被设计为包含并基于状态呈现多个项目,那就是 StateListDrawable ,这是当您膨胀 < 时所得到的。 /代码>。所有其他 Drawable 实例都只是该集合的成员或独立绘制。

另请注意,膨胀的 项实际上是 GradientDrawable 而不是 ShapeDrawable。如果您查看 GradientDrawableinflate() 方法在源代码中,您可以获得有关如何使用每个属性的所有详细信息。

哈!

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.

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 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.

HTH!

陌伤浅笑 2024-12-23 20:12:25

事实上,您可以将 ColorStateList 指定为 shape 的 xml 内的纯色 -> GradientDrawable,但这只是一个 新功能棒棒糖

旧版本的 GradientDrawable 仅接受颜色资源。

如果您有兴趣,目前正在研究兼容的替代方案。

In fact you can assign a ColorStateList as a solid color inside of the xml of a a shape -> 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.

爱的那么颓废 2024-12-23 20:12:25

您正在这样做......只需将其替换

   android:color="@color/color_stateful"

android:background="@color/color_stateful"

update:

在 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:background="@color/color_stateful" /> <!--this is the chanage i made... here-->
        </shape>
    </item>

    <item android:drawable="@drawable/selector_png_drawable" />
</layer-list>

You are doing it wring .... just Replace this

   android:color="@color/color_stateful"

with

android:background="@color/color_stateful"

update:

in your program code in the 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:background="@color/color_stateful" /> <!--this is the chanage i made... here-->
        </shape>
    </item>

    <item android:drawable="@drawable/selector_png_drawable" />
</layer-list>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文