zoukankan      html  css  js  c++  java
  • Flex 学习笔记------自定义按钮状态切换样式

    自定义按钮状态切换样式

    Flex 中按钮的状态主要分为四种,up、down、over、disabled。在Flex 3.x的版本中,可以给 Button 组件绑定四种不同的icon:upIcon、downIcon、overIcon、disabledIcon。

    .button{
       upIcon: Embed(source='imgurl'),      
       downIcon: Embed(source='imgurl'),    
       overIcon: Embed(source='imgurl'),    
       disabledIcon: Embed(source='imgurl')    
    }

    但是在Flex4(SDK 4.6)中却找不到这四个style属性了。于是乎,另寻它途吧。

    方法一:事件法

    既然没有直接的状态属性,那就在具体的事件响应中设置了。还好Button的 icon 属性还在,

    package utilX {
    import spark.components.Button;
    
    // 这里设置 IconButton 的 Style [Style(name
    ="iconSkinDisabled")] [Style(name="iconSkinDown")] [Style(name="iconSkinOver")] [Style(name="iconSkinUp")]public class IconButton extends Button{   // 构造函数 public function IconButton(){ super(); addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void{ e.target.setStyle("icon", e.target.getStyle('iconSkinDown')); }); addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent):void{ e.target.setStyle("icon", e.target.getStyle('iconSkinUp')); }); addEventListener(MouseEvent.MOUSE_OVER, function(e:MouseEvent):void{ e.target.setStyle("icon", e.target.getStyle('iconSkinOver')); }); } } }

    方法二: skin 类

    Flex4 中增加了一个 skin 类,用来控制一些样式的显示。首先需要定义一个 SparkShin 类:

    <?xml version="1.0"?>
    <s:SparkSkin
            xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/halo"
            currentStateChanging="onCurrentStateChanging(event)"
            >
      // 定义宿主 
      // IconButton中可以增加一些额外的样式属性
    <fx:Metadata> [HostComponent("utilX.IconButton")] </fx:Metadata> <fx:Script><![CDATA[ import mx.events.StateChangeEvent; import mx.managers.CursorManager; private function onCurrentStateChanging(event:StateChangeEvent):void { switch (event.newState) { case "up": setIcon("iconSkinUp"); break; case "over": setIcon("iconSkinOver"); break; case "down": setIcon("iconSkinDown"); break; case "disabled": setIcon("iconSkinDisabled"); break; } } private function setIcon(type:String):void { if (hostComponent.getStyle(type) != null) { icon.source = hostComponent.getStyle(type); } } ]]></fx:Script> <s:states> <s:State name="up"/> <s:State name="over"/> <s:State name="down"/> <s:State name="disabled"/> </s:states> <s:Rect left="0" right="0" top="0" bottom="0" width="{hostComponent.getStyle('width')}" height="{hostComponent.getStyle('height')}" radiusX="2" radiusY="2"> <s:stroke> <s:SolidColorStroke id="outline" weight="1"/> </s:stroke> </s:Rect> <s:Group horizontalCenter="0" verticalCenter="0"> <s:Image id="icon" width="{hostComponent.getStyle('width')}" height="{hostComponent.getStyle('height')}" source="{hostComponent.getStyle('iconSkinDown')}" verticalCenter="0" /> </s:Group> </s:SparkSkin>

    这种方式更加灵活,理论上可定制性更强。包括按钮的形状大小颜色都可以在 skin 中搞定。

    具体使用方式:

    IconButton.as

    package utilX {
    import spark.components.Button;
    
    // 这里设置 IconButton 的 Style
    [Style(name="iconSkinDisabled")]
    [Style(name="iconSkinDown")]
    [Style(name="iconSkinOver")]
    [Style(name="iconSkinUp")]
    
    public class IconButton extends Button{
      // 构造函数
        public function IconButton(){
            super();
            });
        }
    }
    }

    Application.mxml

    <?xml version="1.0"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   xmlns:view="viewX.*"
                   fontFamily="Microsoft YaHei" width="100%">
       <fx:Style>
            .button{
                  iconSkinUp: Embed(source='imgurl'),
                  iconSkinDown: Embed(source='imgurl'),
                  iconSkinOver: Embed(source='imgurl'),
                  iconSkinDisabled: Embed(source='imgurl')
             }
       </fx:Style>    
        <view: IconButton width="20" height="20" styleName="button"  />
    </s>                
    -----------------------------一花开五叶 结果自然成-------------------------------------------------
  • 相关阅读:
    文件下载(Servlet/Struts2)
    Spring学习(一)---依赖注入和控制反转
    MyBatis学习(三)---MyBatis和Spring整合
    MyBatis学习(二)---数据表之间关联
    MyBatis学习(一)---配置文件,Mapper接口和动态SQL
    转载:常见端口介绍
    CentOS7 yum提示:another app is currently holding the yum lock;waiting for it to exit
    批量删除文件,只保留目录下最新的几个文件,其他均删除
    转载:SQL Server 如何设置数据库的默认初始大小和自动增长大小
    阿里云ECS使用秘钥或者密码登录
  • 原文地址:https://www.cnblogs.com/zyc-undefined/p/3245661.html
Copyright © 2011-2022 走看看