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 );
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章