方法一:派生mx.controls.Button类
package com
{
import mx.controls.Button;
public class ImageButton extends Button
{
[Embed(source="assets/topmenubtn.png")]
private var icon:Class;
[Embed(source="assets/topmenubtndown.png")]
private var overIcon:Class;
[Embed(source="assets/topmenubtndown.png")]
private var downIcon:Class;
public function ImageButton()
{
super();
this.label = "";
this.width = 16;
this.height = 16;
this.setStyle("cornerRadius",8);
this.setStyle("icon",icon);
this.setStyle("downIcon",downIcon);
this.setStyle("overIcon",overIcon);
this.setStyle("upIcon",icon);
}
}
}
方法2:设置样式,这种方法更灵活
<fx:Style >
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.myCustomButton {
overSkin:Embed(source="assets/topmenubtn.png");
upSkin:Embed(source="assets/topmenubtn.png");
downSkin:Embed(source="assets/topmenubtndown.png");
}
</fx:Style >
var te:mx.controls.Button= new mx.controls.Button();
te.width=128;
var residandname:Array=array[i].split("|");
te.label = residandname[1];
te.accessibilityDescription=residandname[0];
te.accessibilityShortcut=residandname[2];
te.addEventListener(MouseEvent.CLICK, handleClick);
te.styleName="myCustomButton";