我自己也有类似的问题。这就是我最终使用的:
package {
import mx.containers.HBox;
import mx.containers.VBox;
import mx.events.FlexEvent;
public class MultiColumnVBox extends HBox {
// public properties
public var columnWidth:int;
public var verticalGap:int;
public var adjustForScrollbar:Boolean;
public function MultiColumnVBox() {
super();
this.addEventListener(FlexEvent.CREATION_COMPLETE, rearrangeChildren);
}
private function rearrangeChildren(evtObj:FlexEvent):void {
// height will change whilst rearranging children, as will the Children Array
// we store them once at the start
var myHeight:int = this.height;
var children:Array = this.getChildren();
var lastIndex:int = 0;
var vBoxIndex:int = 0;
var vBox:VBox;
var totalHeight:int = -this.verticalGap + (this.adjustForScrollbar ? 16 : 0);
for(var i:int=0; i<children.length; i++) {
// resize each child and measure the height
// if you don't want it resized to the columnWidth, set the maxWidth property
children[i].width = this.columnWidth;
children[i].validateSize();
totalHeight += children[i].measuredHeight + this.verticalGap;
// if we're too tall, or we've reached the last element, move them into a VBox
if(totalHeight > myHeight || i == children.length-1) {
vBox = new VBox();
vBox.setStyle("verticalGap", this.verticalGap);
vBox.width = this.columnWidth;
// include last child if there is room
for(var j:int=lastIndex; j<(totalHeight > myHeight ? i : i+1); j++) {
vBox.addChild(children[j]);
}
this.addChildAt(vBox, vBoxIndex);
vBoxIndex += 1;
lastIndex = i;
totalHeight = -this.verticalGap + (this.adjustForScrollbar ? 16 : 0);
}
}
}
}
}
然而,最终结果略有不同。它不是将孩子移动到交替列,而是无限期地填充第一列的高度,然后是第二列,然后是第三列,依此类推。
如果您打算像您所说的那样创建我们的类,则应该采用类似的方法:等待 CREATION_COMPLETE 事件,然后将子项重新排列为两个 VBox 控件。