如何创建带有 3 个标签的自定义 Flex 4 按钮

发布于 2024-10-21 22:29:34 字数 2065 浏览 7 评论 0原文

我想创建一个带有三个标签的自定义按钮组件:左对齐、中对齐和右对齐。我不能只使用标签对齐属性,因为我想同时使用所有 3 个标签。

我熟悉创建自定义组件,但我以前从未尝试过构建这样的组件...

到目前为止,这是我所拥有的:

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

    <fx:Script>
        <![CDATA[

            private var labelContentL:String;
            private var labelContentC:String;
            private var labelContentR:String;

            public function set labelL(value:String):void
            {
                labelContentL = value;
            }
            public function set labelC(value:String):void
            {
                labelContentC = value;
            }
            public function set labelR(value:String):void
            {
                labelContentR = value;
            }

            public function get labelL():String
            {
                return labelContentL;
            }
            public function get labelC():String
            {
                return labelContentC;
            }
            public function get labelR():String
            {
                return labelContentR;
            }

        ]]>
    </fx:Script>

    <s:Label id="l" width="100%" text="{labelContentL}" textAlign="left" paddingLeft="10" />
    <s:Label id="c" width="100%" text="{labelContentC}" textAlign="center" />
    <s:Label id="r" width="100%" text="{labelContentR}" textAlign="right" paddingRight="10" />

</s:Button>

创建按钮后标签不会更改,所以我不担心关于缺少的[Bindable]元数据。

我现在陷入困境,收到以下编译器错误:

“String”类型的默认属性“label”的多个初始值设定项值。

...对于 3 行中的每一行。

基于这个答案 对于类似的问题,我尝试将 label="" 添加到我的 声明中,但这只会增加另一个错误。

我该如何解决这个问题?

I would like to create a custom Button component with three labels: left-, center-, and right-justified. I can't just use the label justification property, because I want to use all 3 labels at the same time.

I'm familiar with creating custom components, but I've never tried to build one quite like this before...

Here's what I have so far:

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

    <fx:Script>
        <![CDATA[

            private var labelContentL:String;
            private var labelContentC:String;
            private var labelContentR:String;

            public function set labelL(value:String):void
            {
                labelContentL = value;
            }
            public function set labelC(value:String):void
            {
                labelContentC = value;
            }
            public function set labelR(value:String):void
            {
                labelContentR = value;
            }

            public function get labelL():String
            {
                return labelContentL;
            }
            public function get labelC():String
            {
                return labelContentC;
            }
            public function get labelR():String
            {
                return labelContentR;
            }

        ]]>
    </fx:Script>

    <s:Label id="l" width="100%" text="{labelContentL}" textAlign="left" paddingLeft="10" />
    <s:Label id="c" width="100%" text="{labelContentC}" textAlign="center" />
    <s:Label id="r" width="100%" text="{labelContentR}" textAlign="right" paddingRight="10" />

</s:Button>

The labels won't change after the button is created, so I'm not worried about the missing [Bindable] metadata.

I'm stuck right now, getting the following compiler error:

Multiple initializer values for default property, 'label', of type 'String'.

...for each of the 3 <s:Label> lines.

Based on this answer to a similar question, I tried adding label="" to my <s:Button> declaration, but that just adds another error.

How do I fix this?

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

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

发布评论

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

评论(1

栩栩如生 2024-10-28 22:29:34

您的问题是按钮标签下名为 label 的标签不是标签类型的项目,它是按钮上使用的标签,并且是字符串类型。

为什么不将其作为皮肤而不是自定义组件呢?

<?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" 
    xmlns:mx="library://ns.adobe.com/flex/mx">
<!-- host component -->
<fx:Metadata>
    [HostComponent("ThreeLabelButton")]
</fx:Metadata>

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

<s:Rect bottomLeftRadiusX="5" 
        bottomRightRadiusX="5" 
        topLeftRadiusX="5" 
        topRightRadiusX="5" 
        top="0" left="0" right="0" bottom="0">
    <s:fill>
        <s:SolidColor color.up="#CCCCCC" color.over="#555555" color.down="#888888" />
    </s:fill>
</s:Rect>
<mx:Label id="leftLabel" text="{hostComponent.leftText}" left="0" />
<mx:Label id="rightLabel" text="{hostComponent.rightText}" right="0" />
<mx:Label id="centerLabel" text="{hostComponent.centerText}" left="{(this.width - centerLabel.width) / 2}" /> 
</s:Skin>

这将适用于此类:

import mx.controls.Label;

import spark.components.supportClasses.ButtonBase;

public class ThreeLabelButtonComponent extends ButtonBase
{
    public function ThreeLabelButtonComponent()
    {
        super();
    }

    [SkinPart]
    public var leftLabel:Label;

    [SkinPart]
    public var rightLabel:Label;

    [SkinPart]
    public var centerLabel:Label;

            [Bindable]
    public var leftText:String;
            [Bindable]
    public var rightText:String;
            [Bindable]
    public var centerText:String;


    protected override function partAdded(partName:String, instance:Object):void
    {
        super.partAdded(partName, instance);
        if(instance === leftLabel)
        {
            leftLabel.text = leftText;  
        }
        if(instance === rightLabel)
        {
            rightLabel.text = rightText;    
        }
        if(instance === centerLabel)
        {
            centerLabel.text = centerText;  
        }
    }
}

Your problem is that a tag named label under the button tag isn't an item of type label, it's the label used on the button, and is of type string.

Why not do it as a skin, rather than a custom component?

<?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" 
    xmlns:mx="library://ns.adobe.com/flex/mx">
<!-- host component -->
<fx:Metadata>
    [HostComponent("ThreeLabelButton")]
</fx:Metadata>

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

<s:Rect bottomLeftRadiusX="5" 
        bottomRightRadiusX="5" 
        topLeftRadiusX="5" 
        topRightRadiusX="5" 
        top="0" left="0" right="0" bottom="0">
    <s:fill>
        <s:SolidColor color.up="#CCCCCC" color.over="#555555" color.down="#888888" />
    </s:fill>
</s:Rect>
<mx:Label id="leftLabel" text="{hostComponent.leftText}" left="0" />
<mx:Label id="rightLabel" text="{hostComponent.rightText}" right="0" />
<mx:Label id="centerLabel" text="{hostComponent.centerText}" left="{(this.width - centerLabel.width) / 2}" /> 
</s:Skin>

This will work with this class:

import mx.controls.Label;

import spark.components.supportClasses.ButtonBase;

public class ThreeLabelButtonComponent extends ButtonBase
{
    public function ThreeLabelButtonComponent()
    {
        super();
    }

    [SkinPart]
    public var leftLabel:Label;

    [SkinPart]
    public var rightLabel:Label;

    [SkinPart]
    public var centerLabel:Label;

            [Bindable]
    public var leftText:String;
            [Bindable]
    public var rightText:String;
            [Bindable]
    public var centerText:String;


    protected override function partAdded(partName:String, instance:Object):void
    {
        super.partAdded(partName, instance);
        if(instance === leftLabel)
        {
            leftLabel.text = leftText;  
        }
        if(instance === rightLabel)
        {
            rightLabel.text = rightText;    
        }
        if(instance === centerLabel)
        {
            centerLabel.text = centerText;  
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文