flex4 button加图标2

  编写一个buttonSkin.mxml,里面使用BitmapFill将图片引入,然后再在按钮的MXMl文件中使用按钮的SkinClass属性将这个buttonSkin.mxml引入。例:
 
   buttonSkin.mxml

   <?xml version="1.0" encoding="utf-8"?>
   <s:Skin xmlns:fx="<a href="http://ns.adobe.com/mxml/2009">http://ns.adobe.com/mxml/2009</a>"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/mx">
        <!-- host component -->
        <fx:Metadata>
                [HostComponent("spark.components.Button")]
        </fx:Metadata>
        <fx:Script>
                <![CDATA[
                        import mx.events.FlexEvent;
 
                        protected function state1_enterStateHandler(event:FlexEvent):void
                        {
                              
                        }
 
                ]]>
        </fx:Script>
      
        <s:states>
                <s:State name="disabled" enterState="state1_enterStateHandler(event)"/>
                <s:State name="down"/>
                <s:State name="over"/>
                <s:State name="up" />
        </s:states>
      
        <s:Rect left="0" right="0" top="0" bottom="0" radiusX="1" radiusY="1" rotation="0" alpha="2"  >
                <s:fill>
              
                        <mx:BitmapFill source="@Embed('/img/MC301000202/set.png')"  fillMode="clip"   />
                </s:fill>
              
      
        </s:Rect>
</s:Skin>

    test.mxml

    <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication xmlns:fx="<a href="http://ns.adobe.com/mxml/2009">http://ns.adobe.com/mxml/2009</a>"
                                           xmlns:s="library://ns.adobe.com/flex/spark"
                                           xmlns:mx="library://ns.adobe.com/flex/mx">
        <s:layout>
                <s:BasicLayout/>
        </s:layout>
        <fx:Declarations>
                <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        </fx:Declarations>
        <fx:Script>
                <![CDATA[
                        import mx.controls.Alert;
                        protected function myClickHandler():void  {
                                Alert.show("Thanks for submitting.")
                        }  ]]>
        </fx:Script>
        <s:Button id="iconButton"  width="100"  height="30"  x="10"  y="10"  label="Submit to"  skinClass="buttonSkin"  click="myClickHandler()"/>
     </s:WindowedApplication>

你可能感兴趣的:(button)