如果您查看 的measure
方法mx.controls.ComboBase
,您会看到组合框将其计算measuredMinWidth
为文本宽度和组合框按钮宽度之和。
// Text fields have 4 pixels of white space added to each side
// by the player, so fudge this amount.
// If we don't have any data, measure a single space char for defaults
if (collection && collection.length > 0)
{
var prefSize:Object = calculatePreferredSizeFromData(collection.length);
var bm:EdgeMetrics = borderMetrics;
var textWidth:Number = prefSize.width + bm.left + bm.right + 8;
var textHeight:Number = prefSize.height + bm.top + bm.bottom
+ UITextField.TEXT_HEIGHT_PADDING;
measuredMinWidth = measuredWidth = textWidth + buttonWidth;
measuredMinHeight = measuredHeight = Math.max(textHeight, buttonHeight);
}
calculatePreferredSizeFromData
@defmeta 提到的方法(在 中实现mx.controls.ComboBox
)假设数据渲染器只是一个文本字段,并用于从对象flash.text.lineMetrics
中的标签字段计算文本宽度data
。如果您想向项目渲染器添加一个额外的视觉元素,并ComboBox
在计算它自己的大小时考虑它的大小,您将必须扩展mx.controls.ComboBox
类并重写该calculatePreferredSizeFromData
方法,如下所示:
override protected function calculatePreferredSizeFromData(count:int):Object
{
var prefSize:Object = super.calculatePrefferedSizeFromData(count);
var maxW:Number = 0;
var maxH:Number = 0;
var bookmark:CursorBookmark = iterator ? iterator.bookmark : null;
var more:Boolean = iterator != null;
for ( var i:int = 0 ; i < count ; i++)
{
var data:Object;
if (more) data = iterator ? iterator.current : null;
else data = null;
if(data)
{
var imgH:Number;
var imgW:Number;
//calculate the image height and width using the data object here
maxH = Math.max(maxH, prefSize.height + imgH);
maxW = Math.max(maxW, prefSize.width + imgW);
}
if(iterator) iterator.moveNext();
}
if(iterator) iterator.seek(bookmark, 0);
return {width: maxW, height: maxH};
}
如果可能,将图像尺寸存储在数据对象中并将这些值用作imgH
和imgW
,这将使调整大小更加容易。
编辑:
maxH
如果您要向渲染中添加除图像之外的元素(如标签),您还必须在遍历数据元素时计算它们的大小,并在计算和时考虑这些尺寸maxW
。