在 CSS 上嵌入图像以形成扩展的 Spark 按钮 (IconButton)

发布于 2024-10-28 19:23:42 字数 2388 浏览 5 评论 0原文

我有这个 IconButton:

Class

package extra {

    import spark.components.Button;
    import spark.primitives.BitmapImage;

    public class IconButton extends Button {

        private var _icon:Class;

        [SkinPart(required="false")]
        public var iconElement:BitmapImage;

        public function get icon():Class {

            return _icon;

        }

        public function set icon(val:Class):void {

            _icon = val;

            if (iconElement != null) {

                iconElement.source = _icon;

            }

        }

        override protected function partAdded(partName:String, instance:Object):void {

            super.partAdded(partName, instance);

            if (icon !== null && instance == iconElement) {

                iconElement.source = icon;

            }

        }

    }

}

Skin

<?xml version="1.0" encoding="utf-8"?>
<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" minWidth="40" minHeight="22">

    <fx:Metadata>
       [HostComponent("spark.components.Button")]
    </fx:Metadata>

    <s:states>
        <s:State name="up" />
        <s:State name="over" />
        <s:State name="down" />
        <s:State name="disabled" />
    </s:states>

    <!-- background -->
    <s:BitmapImage id="iconElement" width="104" height="54"/>

    <!-- label -->
    <s:Label
    id="labelDisplay"
    textAlign="center"
    color="0xffffff"
    fontFamily="AudiNormal"
    fontWeight="bold"
    fontSize="15"
    width="100%"
    top="64"
    />
</s:Skin>

CSS

e|IconButton {
    skinClass: ClassReference('skins.IconButton');
}

非常适合:

MXML

<extra:IconButton icon="@Embed('../assets/img/animals/dog.png')">Dog Name</extra:IconButton>

但是我该怎么做呢?

MXML

<extra:IconButton styleName="dog">Dog Name</extra:IconButton>

CSS

e|IconButton {
    skinClass: ClassReference('skins.IconButton');
}

.dog {
    icon: Embed('../assets/img/animals/dog.png');
}

I have this IconButton:

Class

package extra {

    import spark.components.Button;
    import spark.primitives.BitmapImage;

    public class IconButton extends Button {

        private var _icon:Class;

        [SkinPart(required="false")]
        public var iconElement:BitmapImage;

        public function get icon():Class {

            return _icon;

        }

        public function set icon(val:Class):void {

            _icon = val;

            if (iconElement != null) {

                iconElement.source = _icon;

            }

        }

        override protected function partAdded(partName:String, instance:Object):void {

            super.partAdded(partName, instance);

            if (icon !== null && instance == iconElement) {

                iconElement.source = icon;

            }

        }

    }

}

Skin

<?xml version="1.0" encoding="utf-8"?>
<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" minWidth="40" minHeight="22">

    <fx:Metadata>
       [HostComponent("spark.components.Button")]
    </fx:Metadata>

    <s:states>
        <s:State name="up" />
        <s:State name="over" />
        <s:State name="down" />
        <s:State name="disabled" />
    </s:states>

    <!-- background -->
    <s:BitmapImage id="iconElement" width="104" height="54"/>

    <!-- label -->
    <s:Label
    id="labelDisplay"
    textAlign="center"
    color="0xffffff"
    fontFamily="AudiNormal"
    fontWeight="bold"
    fontSize="15"
    width="100%"
    top="64"
    />
</s:Skin>

CSS

e|IconButton {
    skinClass: ClassReference('skins.IconButton');
}

That works great with:

MXML

<extra:IconButton icon="@Embed('../assets/img/animals/dog.png')">Dog Name</extra:IconButton>

But how can I do this?:

MXML

<extra:IconButton styleName="dog">Dog Name</extra:IconButton>

CSS

e|IconButton {
    skinClass: ClassReference('skins.IconButton');
}

.dog {
    icon: Embed('../assets/img/animals/dog.png');
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

Saygoodbye 2024-11-04 19:23:42

删除图标属性并使用样式代替:

[Style(name="icon", type="Class", inherit="no")]

在您的皮肤文件中:

<s:BitmapImage id="iconElement" width="104" height="54" icon="{hostComponent.getStyle('icon')}/>

只是猜测,但尚未测试......

Remove icon property and use a style instead :

[Style(name="icon", type="Class", inherit="no")]

In your skin file :

<s:BitmapImage id="iconElement" width="104" height="54" icon="{hostComponent.getStyle('icon')}/>

Just a guess, haven't tested it though...

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