---
title: "flex4中使用backgroundImage"
author: "liaoshaoyao"
date: "2011-04-20T17:04:10Z"
tags: ["Adobe", "BBS", "CSS", "Flash", "XML"]
categories: ["Web前端"]
slug: "flex4-backgroundimage"
draft: false
lastmod: "2011-04-20T17:04:10Z"
id: 1009995
url: "https://sunshineyao.iteye.com/blog/1009995"
---

1、 方式一
可以用BorderContainer把内容包涵进去


<s:BorderContainer borderVisible="false" height="100%" width="100%" styleName="mainbg">


<你的其它标签>


</s:BorderContainer>


在CSS里面定义

.mainbg{  
   background-size: "100%";
   background-image:Embed("图片路径");
} 
经使用，但觉效果没能如愿， 没有完全铺满

2、 参考网址：
http://bbs.airia.cn/Flash_Builder_4/thread-8867-2-1.html

3、 方式3：

今天将项目适移到了Gumbo也就是Flex4上面，结果所有原来设置了背景图片的控件现在背景图片全都显示不出来了，并且原来的CSS也都失去的效用。。。。
解决方法如下：
在CSS文件最开始增加：@namespace "http://www.adobe.com/2006/mxml"; 
也就是在FLEX4中，CSS也是有命名空间的了
然后，就是最重要的背景图片显示问题，我在网上搜了小半天，终于找到了解决方法：
1：写Skin文件：LoginAppliactionSkin 内容如下：
<?xml version="1.0" encoding="utf-8"?>
<s:SparkSkin
 xmlns:fx="http://ns.adobe.com/mxml/2009"
 xmlns:mx="library://ns.adobe.com/flex/halo"
 xmlns:s="library://ns.adobe.com/flex/spark"
 alpha.disabled="0.5">
 <fx:Metadata>
  [HostComponent("spark.components.SkinnableContainer")]
 </fx:Metadata>
 <s:states>
  <s:State name="normal" />
  <s:State name="disabled" />
 </s:states>
 <s:Rect left="0" right="0" top="0" bottom="0">
  <s:stroke>
   <s:SolidColorStroke color="0" alpha="0.50" weight="3" />
  </s:stroke>
 </s:Rect>
 <s:Rect id="background" left="3" top="3" right="3" bottom="3" alpha="1.0">
  <s:fill>
   <s:BitmapFill source="@Embed(source='skins/default/images/loginBack.png')"/>
  </s:fill>
 </s:Rect>
 <s:Group id="contentGroup" left="10" right="10" top="10" bottom="10">
  <s:layout>
   <s:VerticalLayout/>
  </s:layout>
 </s:Group>
</s:SparkSkin>
然后在CSS文件中增加：
Login{
 borderSkin:ClassReference( 'com.v246.skins.defaults.LoginAppliactionSkin' );
}
OK，现在背景图片好使了，，

另外，现在的Effect也不好使了，正在找解决方法


方式4：
<s:Group x="0" y="0" height="100%" width="100%">
 <s:Rect x="0" y="0" height="100%" width="100%">
  <s:fill>
   <s:BitmapFill source="@Embed('../pic/001.jpg')"/>
  </s:fill>
 </s:Rect>
</s:Group>

已经测试，group相当于canvas，而后里面画一个矩形，在矩形里面用图片填充，也可以使用渐变色填充，如下：

<s:Rect alpha="0.5" x="0" y="0" width="100%" height="100%">
 <s:fill>
  <s:LinearGradient rotation="250">
   <s:GradientEntry color="0x222222" ratio="0"/>
   <s:GradientEntry color="0xdddddd" ratio="1"/>
  </s:LinearGradient>
 </s:fill>
</s:Rect> 