在某次进行动态为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 --