Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3351731
  • 博文数量: 530
  • 博客积分: 13360
  • 博客等级: 上将
  • 技术积分: 5473
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-13 13:32
文章分类

全部博文(530)

文章存档

2017年(1)

2015年(2)

2013年(24)

2012年(20)

2011年(97)

2010年(240)

2009年(117)

2008年(12)

2007年(8)

2006年(9)

分类: Web开发

2013-03-30 15:23:39

转载:http://www.cnblogs.com/spoony/archive/2010/11/08/flex4-skinning-part04-notecard-demo.html
1.概述

      上一篇文章《FLEX皮肤实例4_skinpart

      本章描述了如何自定义一个组件“留言板”,并使用skin part。留言板效果如下


2.定义组件NoteCard
2.1 必须继承父类SkinableComponent
      留言板NoteCard, 继承skinnableComponent父类。

2.2 定义二个状态
      留言板具有二个状态:normal和disabled

2.3 定义skin part
      留言条由文本标签和圆形按钮组成,因此这里对其进行分别定义为labelDisplay和closeButton, 类型分别为TextBase和Button.
      labelDisplay必须在skin文件中有对应id的标签。
      closeButton为可选的。

      [SkinPart(required="true")]
     public var labelDisplay:TextBase;
 
     [SkinPart(required="true")]
     public var closeButton:Button;

2.4 定义变量
      变量_text保存留言板中的内容。

点击(此处)折叠或打开

  1. private var _text:String;
  2. public function get text():String{
  3.     return _text;
  4. }
  5.  
  6. public function set text(value:String):void{
  7.     if(_text == value)
  8.         return;
  9.     _text = value;
  10.    
  11.     //修改皮肤中显示的值
  12.     if(labelDisplay)
  13.         labelDisplay.text = value;
  14. }

2.5 重载skin part的行为
       由于skin可以在运行时加载或者卸载,因此需要对相应skin part属性进行动态设置。Flex框架会将skin中声明的skin part与component中的相关属性结合起来,并通过skinning生命周期中相应的方法进行通知。
  这里的NoteCard类用到的相关方法主要有getCurrentSkinState(), partAdded(), partRemoved()这三个。这三个方法都在SkinnableComponent中有定义,因此只需要在子类中进行覆写。

getCurrentSkinState()方法是返回要要加载skin的component的state名称。比如这里的NoteCard类有disabled, enabled,
实现代码如下:
  1. override protected function getCurrentSkinState():String{
  2.     if(!enabled)
  3.         return "disabled";
  4.      
  5.         return "normal";
  6. }

partAdded()方法会在skin part添加的时候被调用,因此在这个方法中,我们可以添加这个skin part的相关处理逻辑,比如事件监听等
实现代码如下:

  1. override protected function partAdded(partName:String, instance:Object):void{
  2.     super.partAdded(partName, instance);
  3.      
  4.     if(instance == labelDisplay)
  5.         labelDisplay.text = _text;
  6.      
  7.     if(instance == closeButton)
  8.         closeButton.addEventListener(MouseEvent.CLICK, closeButton_clickHandler);
  9. }

partRemoved()方法会在skin part移除的时候被调用,因此在这个方法中,我们可以将添加在这个skin part的相关处理逻辑进行移除,比如事件监听等。
实现代码如下:
  1. override protected function partRemoved(partName:String, instance:Object):void{
  2.     super.partRemoved(partName, instance);
  3.      
  4.     if(instance == closeButton)
  5.         closeButton.removeEventListener(MouseEvent.CLICK, closeButton_clickHandler);
  6. }
closeButton_clickHandler事件主要是将先前添加的留言条进行移除
代码如下
  1. protected function closeButton_clickHandler(event:MouseEvent) : void{
  2.       event.stopPropagation();
  3.       IVisualElementContainer(parent).removeElement(this);
  4. }

下面给来NoteCard完整代码

点击(此处)折叠或打开

  1. import flash.events.MouseEvent;
  2.     
  3.     import mx.core.IVisualElementContainer;
  4.     
  5.     import spark.components.Button;
  6.     import spark.components.supportClasses.SkinnableComponent;
  7.     import spark.components.supportClasses.TextBase;
  8.     
  9.     [SkinState("normal")]
  10.     [SkinState("disabled")]
  11.     public class NoteCard extends SkinnableComponent
  12.     {
  13.         public function NoteCard()
  14.         {
  15.             super();
  16.         }
  17.         
  18.         [SkinPart(required="true")]
  19.         public var labelDisplay:TextBase;
  20.         
  21.         [SkinPart(required="false")]
  22.         public var closeButton:Button;
  23.         
  24.         private var _text:String;
  25.         
  26.         public function get text():String
  27.         {
  28.             return _text;
  29.         }
  30.         
  31.         public function set text(value:String):void
  32.         {
  33.             if (_text == value)
  34.                 return;
  35.             
  36.             _text = value;
  37.             
  38.             if (labelDisplay)
  39.                 labelDisplay.text = value;
  40.         }
  41.         
  42.         override public function set enabled(value:Boolean) : void
  43.         {
  44.             if (enabled != value)
  45.                 invalidateSkinState();
  46.             
  47.             super.enabled = value;
  48.         }
  49.         
  50.         override protected function getCurrentSkinState() : String
  51.         {
  52.             if (!enabled)
  53.                 return "disabled";
  54.             
  55.             return "normal"
  56.         }
  57.         
  58.         override protected function partAdded(partName:String, instance:Object) : void
  59.         {
  60.             super.partAdded(partName, instance);
  61.             
  62.             if (instance == labelDisplay)
  63.                 labelDisplay.text = _text;
  64.             
  65.             if (instance == closeButton)
  66.                 closeButton.addEventListener(MouseEvent.CLICK, closeButton_clickHandler);
  67.         }
  68.         
  69.         override protected function partRemoved(partName:String, instance:Object) : void
  70.         {
  71.             super.partRemoved(partName, instance);
  72.             
  73.             if (instance == closeButton)
  74.                 closeButton.removeEventListener(MouseEvent.CLICK, closeButton_clickHandler);
  75.         }
  76.         
  77.         protected function closeButton_clickHandler(event:MouseEvent) : void
  78.         {
  79.             event.stopPropagation();
  80.             
  81.             IVisualElementContainer(parent).removeElement(this);
  82.         }
  83.         
  84.     }



3.定义组件NoteCard的皮肤
3.1 定义NoteCard皮肤
NoteCardSkin

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Skin xmlns:fx=""
  3.         xmlns:s="library://ns.adobe.com/flex/spark"
  4.         xmlns:mx="library://ns.adobe.com/flex/mx">
  5.     <!-- host component -->
  6.     <fx:Metadata>
  7.         [HostComponent("note.NoteCard")]
  8.     </fx:Metadata>
  9.     <!-- states -->
  10.     <s:states>
  11.         <s:State name="normal"/>
  12.         <s:State name="disabled"/>
  13.     </s:states>
  14.     <!-- -->
  15.     <s:Rect width="300" height="200">
  16.         <s:stroke>
  17.             <s:SolidColorStroke color="0xAA8E5E" weight="4"/>
  18.         </s:stroke>
  19.         <s:fill>
  20.             <s:LinearGradient>
  21.                 <s:GradientEntry color="0x60c2fe"/>
  22.                 <s:GradientEntry color="0x70b2ee"/>
  23.             </s:LinearGradient>
  24.         </s:fill>
  25.     </s:Rect>
  26.     <s:Label id="labelDisplay" left="10" top="10" width="280" height="180" color="0xFFF3E9" lineBreak="toFit" fontSize="18" fontFamily="Chalkboard"/>
  27.     <s:Button id="closeButton" skinClass="note.NoteCardCloseButtonSkin" top="5" right="5"/>
  28. </s:Skin>
closeButton也使用了一个自定义皮肤,NoteCardCloseButtonSkin

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Skin xmlns:fx=""
  3.         xmlns:s="library://ns.adobe.com/flex/spark"
  4.         xmlns:mx="library://ns.adobe.com/flex/mx">
  5.     <!-- host component -->
  6.     <fx:Metadata>
  7.         [HostComponent("spark.components.Button")]
  8.     </fx:Metadata>
  9.      
  10.     <!-- states -->
  11.     <s:states>
  12.         <s:State name="disabled" />
  13.         <s:State name="down" />
  14.         <s:State name="over" />
  15.         <s:State name="up" />
  16.     </s:states>
  17.      
  18.     <s:Ellipse width="16" height="16">
  19.         <s:stroke>
  20.             <s:SolidColorStroke color="0x131313" weight="1" />
  21.         </s:stroke>
  22.         <s:fill>
  23.             <s:LinearGradient>
  24.                 <s:GradientEntry color="0xAA8E5E" color.over="0x9A7E4E" color.down="0x7A5E2E" />
  25.                 <s:GradientEntry color="0xCCBBAA" color.over="0xBCAB9A" color.down="0x9C8B7A"/>
  26.             </s:LinearGradient>
  27.         </s:fill>
  28.     </s:Ellipse>
  29.      
  30.     <s:Label text="X" horizontalCenter="0" verticalCenter="0" />
  31.      
  32. </s:Skin>

4.程序演示

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx=""
  3.              xmlns:s="library://ns.adobe.com/flex/spark"
  4.              xmlns:mx="library://ns.adobe.com/flex/mx"
  5.              xmlns:note="note.*"
  6.              minWidth="955" minHeight="600">
  7.     <fx:Declarations>
  8.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->
  9.     </fx:Declarations>
  10.     
  11.     <note:NoteCard id="card" x="20" y="10" skinClass="note.NodeCardSkin" text="我是中国人"/>
  12. </s:Application>
效果如下



阅读(3073) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

mujinglan682014-05-19 17:00:56

写的真好