`

Flex中自定义组件

    博客分类:
  • flex
 
阅读更多
如要重写组件,就不得不了解Flex中组件初始化过程。初始化过程中,系统依次调用组件的一下方法

Constructor -> createChildren() -> commitProperties() -> measure() -> updateDisplayList()

介绍下各个函数的作用:

Constructor构造器,不用说了吧

createChildren用来创建组件的子对象(比如你在Panel里添加了个Button,就在这时addChild)

commitProperties用来设置各个对象的属性(例如x,y坐标等等)

measure用来计量该组件的大小(以便Flex布局管理器能正确知道该组件的大小,给其分配适当空间)
updateDisplayList用来重绘组件(在组件改变大小、移动位置等时候调用)

添加了可视化元素的自定义组建(说得直白点,就是样子和基类组件不同了),必须要重写createChildren()函数和updateDisplayList()函数。

当自定义的组建需要对传入的某个属性做出反应的话,就应该重写commitProperties函数。

当自定义的组件和基类组件的大小不一致时,就应该重写measure方法,保证提供正确的大小信息。

<--- 注意重写以上方法时,要先调用基类的相应方法 --->

以下是一个自定义组件,在Panel的右上角新加了一个按钮

package com.lheric
{
import mx.containers.Panel;
import mx.controls.Button;

public class MaxRestorePanel extends Panel
{
//新增的按钮
protected var _maxMinBtn:Button;
//按钮与边缘的间隔
protected var _gap:int = 5;


public function MaxRestorePanel()
{
super();
}

protected override function createChildren():void {
//base class
super.createChildren();
//custom
_maxMinBtn = new Button;
_maxMinBtn.width = 50;
_maxMinBtn.height = 10;
rawChildren.addChild( _maxMinBtn );
}

protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
//base class
super.updateDisplayList(unscaledWidth,unscaledHeight);
//custom
//size
_maxMinBtn.setActualSize( 20, 20 );
//position
var x:int = unscaledWidth - _maxMinBtn.width - _gap;
var y:int = _gap;
_maxMinBtn.move( x, y );
}
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics