【问题标题】:Flex unexpected behaviour with class and itemRenderer使用类和 itemRenderer 显示意外行为
【发布时间】:2011-05-27 09:26:16
【问题描述】:

我陷入了我没想到的行为,请解释我以理解并解决它。 - 问题如下:我有一个包含标签的类,这个类在 itemRenderer 中使用,整个工作没有异常,但它不显示标签的文本。 我尝试添加按钮,但问题仍然存在 - 它从未真正添加按钮。

为什么?

public class BusTreeNode extends UIComponent
{
    protected var label : Label;

    public function BusTreeNode()
    {
        super();

        label = new Label();
        this.addChild( label );
        label.x    = 40;
        label.y    = 2; 
        label.text = "test";
    }
}

... itemRenderer 内部:

<sl:BusTreeNode width="100%" />

【问题讨论】:

    标签: apache-flex actionscript-3 itemrenderer


    【解决方案1】:

    我想问题是您的BusTreeNode 类中没有measure() 实现。所以你的组件默认为 0x0 大小。本声明:

    <sl:BusTreeNode width="100%" />
    

    仍然给 0 作为高度。

    您可以阅读更多关于测量here 的 Flex 组件。而this article 对于创建自定义组件非常有用。

    【讨论】:

    • 超级酷。 :) 我终于成功地展示了标签。从来没有想过有这么复杂的覆盖类,在 Flex 4 的渲染器中使用。这对我来说仍然有点混乱,但在这里每天都变得清晰和清晰:D 谢谢。
    • 如果你保持代码简单,那就很简单了。而且我建议您从简单的事情开始,并且只在第一次使用 MXML 渲染器。所以@RIAstar 的分析器也很有意义:)
    【解决方案2】:

    从 spark(或 Flex 3 中的 mx)ItemRenderer 类扩展更容易。像这样创建一个新的 MXML 文件:

    <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                    xmlns:s="library://ns.adobe.com/flex/spark" >
    
        <!-- label -->
        <s:Label id="labelDisplay" />
    
    </s:ItemRenderer>
    

    'labelDisplay' 的 'text' 属性将由它所在的 List(或其他数据组件)自动设置。

    此外,在 Flex 组件生命周期中,应在 createChildren() 方法中将可视元素添加到 displayList 中。所以如果你绝对想用纯ActionScript来写,你可以这样写,但更难:

    public class MyItemRenderer extends ItemRenderer {
    
        private var button:Button;
    
        override protected function createChildren():void {
            super.createChildren();
    
            button = new Button();
            addElement(button);
        }
    
        override public function set label(value:String):void {
            super.label = button.label = value;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      相关资源
      最近更新 更多