关于Scroller.addElement()添加元素报错问题

本文详细阐述了在使用Scroller组件时遇到的ArgumentError问题,通过实例代码展示了如何正确地将组件作为视域添加到Scroller中,避免了错误的发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在某次进行动态为Scroller添加元素时,报错:ArgumentError: This operation is not supported.

因为Scroller只允许添加一个Child,所以先排除是否添加了多个Child,但仍然不行,后来才知道要用viewport来接连组件。

<?xml version="1.0" encoding="utf-8"?>
<s:Application ... creationComplete="init(event)">
  <fx:Script>
    <![CDATA[
      ...      
      protected function init(event:FlexEvent):void
      {
        // TODO Auto-generated method stub        
        var hg:HGroup=new HGroup();
        hg.percentWidth=100;
        hg.height=50;
        hg.paddingBottom=10;
        hg.paddingLeft=hg.paddingRight=5;
        hg.gap=10;
        hg.verticalAlign="middle";
        
        for(var i:int=1;i<20;i++){
          var myBtn:Button=new Button();
          myBtn.width=70;
          myBtn.height=30;
          myBtn.label="btn_"+i;
          hg.addElement(myBtn);
        }
        
        //myScroller.addElement(hg); 这写法运行时报错
        myScroller.viewport=hg;
      }
    ]]>
  </fx:Script>
  
  <s:Group height="80" width="800">
    <s:Rect left="0" top="0" right="0" bottom="0">
      <s:fill>
        <s:SolidColor color="#990000" alpha="0.4" />
      </s:fill>
    </s:Rect>
    <s:Scroller id="myScroller" width="400" height="50" 
          horizontalCenter="0" verticalCenter="0"  />
  </s:Group>
</s:Application>


Scroller 组件显示一个称为视域的单个可滚动组件,以及水平滚动条和垂直滚动条。该视域必须实现 IViewport 接口,其外观必须是 Group 类的派生。
Scroller属性viewport : IViewport 要滚动的视域组件。 

 

-- The End --
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值