我做了很多研究并阅读Adobe Live Docs,直到我的眼睛流血试图解决这个问题。
我正在使用ActionScript构建复合自定义组件,并希望子控件水平布局。这可以通过将它们添加到HGroup
并将HGroup添加到组件来正常工作,问题在于基于百分比的大小调整。
我需要_horizontalGroup:HGroup
根据容器的大小自行调整大小。
单步执行代码表明每个UIComponent的父属性都是......
如果grpControls有一个明确的大小,那么ReportGridSelector也不应该有它的大小吗?
自定义组件就像这样实现...
注意:ReportControl扩展UIComponent并且不包含大小调整逻辑
public class ReportGridSelector extends ReportControl{
/*other display objects*/
private var _horizontalGroup:HGroup;
public function ReportGridSelector(){
super();
percentHeight = 100;
percentWidth = 100;
}
override protected function createChildren():void{
super.createChildren();
if(!_horizontalGroup){
_horizontalGroup = new HGroup();
//I WANT SIZE BY PERCENTAGE, BUT THIS DOESN'T WORK
_horizontalGroup.percentWidth = 100;
_horizontalGroup.percentHeight = 100;
//EXPLICITLY SETTING THEM WORKS, BUT IS STATIC :-(
//_horizontalGroup.width = 200;
//_horizontalGroup.height = 200;
addChild(_horizontalGroup);
}
}
}
使用MXML代码
<?xml version="1.0" encoding="utf-8"?>
<s:VGroup id="grpControls" width="200" height="200">
<ReportControls:ReportGridSelector width="100%" height="100%"/>
</s:VGroup>
如果我明确定义了_horizontalGroup
的{{1}}和width
属性,那么一切都会正常显示。如果我尝试height
或_horizontalGroup.percentWidth
,则会将所有控件放在一起。
有关正在发生的事情的任何想法?
答案 0 :(得分:1)
从updateDisplayList中使显示列表无效时执行布局。
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
_horizontalGroup.width = unscaledWidth;
_horizontalGroup.height = unscaledHeight;
}
了解Flex组件生命周期:
http://livedocs.adobe.com/flex/3/html/help.html?content=ascomponents_advanced_2.html
<强> createChildren 强>
创建组件的所有子组件。例如, ComboBox控件包含一个TextInput控件和一个Button控件 儿童组成部分。
有关更多信息,请参阅实现createChildren()方法。
<强>的updateDisplayList 强>
根据屏幕大小调整组件的子项 在所有以前的属性和样式设置,并绘制任何皮肤或 组件使用的图形元素。的父容器 component确定组件本身的大小。