---
title: "Flex Skin & Style & ItemRenderer"
author: "yoshiyan"
date: "2010-11-20T00:38:50Z"
tags: []
categories: ["Web前端"]
slug: "flex-skin-style-itemrenderer"
draft: false
lastmod: "2011-03-21T16:03:45Z"
id: 816729
url: "https://gexp.iteye.com/blog/816729"
---

<span style="color:red">定义Style的语法</span>

> <fx:Style> 
>     @namespace_declaration 
>     namespace|Application { style_definition } 
> </fx:Style>

示例代码：

```flex
    <fx:Script>
        <![cdata[
            [embed("assets/asterisk_orange.png")]
            protected const ICON:Class;
        ]]>
    </fx:Script>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        s|Button#btn1 {
            icon: "assets/asterisk_orange.png";
        }
 
        s|Button#btn2 {
            icon: Embed("assets/asterisk_orange.png");
        }
 
        s|Button#btn3 {
            icon: PropertyReference("ICON");
        }
    </fx:Style>
 
    <s:Button id="btn1" label="Spark Button with dynamic icon" />
    <s:Button id="btn2" label="Spark Button with inline embedded icon" />
    <s:Button id="btn3" label="Spark Button with metadata [embed] icon" />

```

参考链接：
Using local style definitions
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7e8b.html

<span style="color:red">如何在Skin中访问其对应的host component？</span>
方法1：通过变量hostComponent
示例代码：

```flex
	<s:BitmapImage id="icon" horizontalCenter="0" verticalCenter="0"
				   source="{hostComponent.content}"/>
```

方法2：通过 + 相同ID
示例代码：
Hosted Component:

```flex
[skinpart("false")]
public var bodyGroup:Group;//变量名必须和Skin中的对应组件的ID相同
```


Skin:

```flex
<s:Group id="bodyGroup" width="160" height="160">
	<s:layout>
		<lo:FlowLayout horizontalGap="0" verticalGap="0" padding="0"/>
	</s:layout>
</s:Group>
```


<span style="color:red">如何在ItemRenderer里拿到DataProvider分配的数据？</span>
通过data变量。

<span style="color:red">为什么给Custom ItemRenderer设置的state没有生效。</span>
需要override getCurrentRendererState函数。

```flex

override protected function getCurrentRendererState():String
{
   return this.currentState;
}
```

参考链接：
<http://stackoverflow.com/questions/4557168/spark-list-itemrenderer-focus-defaults-the-currentstate>