要為Tree添加線,可以使用itemRenderer來自定義每個節點的顯示樣式。具體步驟如下:
package
{
import flash.display.Graphics;
import mx.controls.treeClasses.TreeItemRenderer;
public class CustomTreeItemRenderer extends TreeItemRenderer
{
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
var g:Graphics = graphics;
g.clear();
if (data)
{
var tree:Tree = Tree(listData.owner);
var depthColor:uint = getDepthColor(tree, data);
// 繪制線條
g.lineStyle(1, depthColor);
g.moveTo(0, 0);
g.lineTo(tree.indentation, 0);
// 繪制節點圖標
var icon:Class = getStyle("folderIcon");
if (icon)
{
var iconInstance:DisplayObject = new icon();
addChild(iconInstance);
setChildIndex(iconInstance, 0);
iconInstance.x = tree.indentation;
iconInstance.y = (unscaledHeight - iconInstance.height) / 2;
}
}
}
private function getDepthColor(tree:Tree, data:Object):uint
{
// 根據節點深度設置不同的顏色
var depth:int = TreeListData(listData).depth;
var colors:Array = tree.getStyle("lineColors");
return colors[depth % colors.length];
}
}
}
var tree:Tree = new Tree();
tree.itemRenderer = new ClassFactory(CustomTreeItemRenderer);
這樣,每個節點都會繪制一條連接到父節點的線,并顯示相應的節點圖標。您還可以根據需要進一步修改自定義itemRenderer類的樣式和邏輯。