皮肤风格(Style with Skin)

优质
小牛编辑
136浏览
2023-12-01

什么是剥皮?

  • Flex中的外观是一个完全自定义UI组件外观的过程。

  • 皮肤可以定义组件的文本,图像,过滤器,过渡和状态。

  • 可以将外观创建为单独的mxml或ActionScript组件。

  • 使用皮肤,我们可以控制UI组件的所有视觉方面。

  • 对于所有UI组件,定义外观的过程是相同的。

第1步 - 创建一个皮肤

使用File 》 New 》 MXML Skin选项启动“创建MXML外观”向导。

Flex Skin Wizard

输入Package作为cn.xnip.skin ,命名为GradientBackgroundSkin并选择主机组件作为现有的flex BorderContainer控件spark.component.BorderContainer

现在你已经为BorderContainer创建了一个皮肤。 修改mxml皮肤文件src/cn.xnip/skin/GradientBackgroundSkin.mxml

更新填充图层如下 -

<!-- fill -->
<s:Rect id = "backgroundRect" left = "0" right = "0" height = "100%" top = "0">
   <s:fill>
      <s:LinearGradient rotation = "90">
         <s:GradientEntry color = "0x888888" ratio = "0.2" />
         <s:GradientEntry color = "0x111111" ratio = "1" />
      </s:LinearGradient>
   </s:fill>
</s:Rect>	

第2步:涂抹皮肤

您可以通过两种方式在组件上应用皮肤 -

在MXML脚本中应用皮肤(静态)

使用skinClass属性将GradientBackgroundSkin应用于具有id mainContainer的BorderContainer。

<s:BorderContainer width = "560" height = "500" id = "mainContainer" 
   styleName = "container">
   <s:VGroup width = "100%" height = "100%" gap = "50" 
      horizontalAlign = "center" verticalAlign = "middle" 
      skinClass = "cn.xnip.skin.GradientBackgroundSkin">

在ActionScript中应用皮肤(动态)

使用skinClass属性将GradientBackgroundSkin应用于具有id mainContainer的BorderContainer。

protected function gradientBackground_clickHandler(event:MouseEvent):void {
   mainContainer.setStyle("skinClass", GradientBackgroundSkin);
}

Flex样式与皮肤示例

让我们按照以下步骤通过创建测试应用程序来查看Flex应用程序中的皮肤操作 -

描述
1cn.xnip.client包下创建一个名为HelloWorld的项目,如Flex - Create Application一章中所述。
2如上所述,在cn.xnip.skin包下创建外观GradientBackgroundSkin.mxml 。 保持其余文件不变。
3修改HelloWorld.mxml ,如下所述。 保持其余文件不变。
4编译并运行应用程序以确保业务逻辑按照要求运行。

以下是GradientBackgroundSkin.mxml文件的内容src/com/xnip/skin/GradientBackg roundSkin.mxml

<?xml version = "1.0" encoding = "utf-8"?>
<s:Skin xmlns:fx = "http://ns.adobe.com/mxml/2009" 
   xmlns:s = "library://ns.adobe.com/flex/spark" 
   xmlns:mx = "library://ns.adobe.com/flex/mx">
   <!-- host component -->
   <fx:Metadata>
      [HostComponent("spark.components.BorderContainer")]
   </fx:Metadata> 
   <!-- states -->
   <s:states>
      <s:State name = "disabled" />
      <s:State name = "disabled" />
      <s:State name = "normal" />
   </s:states>
   <!-- SkinParts
   name = contentGroup, type = spark.components.Group, required = false
   -->
   <!-- fill -->
   <s:Rect id = "backgroundRect" left = "0" right = "0" height = "100%" top = "0">
      <s:fill>
         <s:LinearGradient rotation = "90">
            <s:GradientEntry color = "0x111111" ratio = "0.2" />
            <s:GradientEntry color = "0x888888" ratio = "1" />
         </s:LinearGradient>
      </s:fill>
   </s:Rect>	
   <!-- must specify this for the host component --> 
   <s:Group id = "contentGroup" left = "0" right = "0" top = "0" bottom = "0" />
</s:Skin>

以下是修改后的HelloWorld.mxml filesrc/com/xnip/client/HelloWorld.mxml

<?xml version = "1.0" encoding = "utf-8"?>
<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"
   width = "100%" height = "100%" minWidth = "500" minHeight = "500"
   initialize = "application_initializeHandler(event)">
   <fx:Style source = "/com/xnip/client/Style.css" />
   <fx:Script>
      <![CDATA[
         import cn.xnip.skin.GradientBackgroundSkin;
         import mx.controls.Alert;
         import mx.events.FlexEvent;
         import spark.skins.spark.BorderContainerSkin;			
         protected function btnClickMe_clickHandler(event:MouseEvent):void {
            Alert.show("Hello World!");				
         }
         protected function application_initializeHandler(event:FlexEvent):void {
            lblHeader.text = "My Hello World Application";				
         }
         protected function gradientBackground_clickHandler(event:MouseEvent):void {
            mainContainer.setStyle("skinClass", GradientBackgroundSkin );
         }
         protected function standardBackground_clickHandler(event:MouseEvent):void {
            mainContainer.setStyle("skinClass", BorderContainerSkin );
         }
      ]]>
   </fx:Script>
   <fx:Declarations>
      <s:RadioButtonGroup id = "selectorGroup" />
   </fx:Declarations>
   <s:BorderContainer width = "500" height = "500" id = "mainContainer"
      skinClass = "spark.skins.spark.BorderContainerSkin" 
      horizontalCenter = "0" verticalCenter = "0" cornerRadius = "10">
      <s:VGroup width = "100%" height = "100%" gap = "50" horizontalAlign = "center"
         verticalAlign = "middle">
         <s:Label id = "lblHeader" fontSize = "40" color = "green" 
            styleName = "heading" />
         <s:Button label = "Click Me!" id = "btnClickMe" 
            click = "btnClickMe_clickHandler(event)" />
         <s:RadioButton color = "gray" fontWeight = "bold" 
            group = "{selectorGroup}" label = "Standard Background" 
            click = "standardBackground_clickHandler(event)" selected = "true" />
         <s:RadioButton color = "gray" fontWeight = "bold" 
            group = "{selectorGroup}" label = "Gradient Background" 
            click = "gradientBackground_clickHandler(event)" />			
      </s:VGroup>			
   </s:BorderContainer>	
</s:Application>

一旦准备好完成所有更改,让我们像在Flex - Create Application章节中那样在正常模式下编译和运行应用程序

Flex Skin Style1

Flex Skin Style2