flex4 css的多种用法

本文通过示例展示了如何在MXML中使用Flex样式,包括如何保存样式为CSS文件,以及如何在MXML文件中引用外部CSS样式,同时说明了运行结果的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址: http://ghostjay.blog.51cto.com/2815221/600912


下面给出一段示例代码:
 
<?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" >
  <s:layout>
    <s:VerticalLayout horizontalAlign="center" paddingTop="20"/>
  </s:layout>
  <fx:Style>
    @namespace mx "library://ns.adobe.com/flex/mx";
    @namespace s "library://ns.adobe.com/flex/spark";
    
    global {
      font-family:Times New Roman, Times, serif;
      color:purple;
    }
    s|Label
    {
      font-size:18;
      font-style:italic;
    }
//descendant selector,要完全符合这样的层次才能有效,见结果
    s|HGroup s|Button s|Label
    {
      font-style:normal;
      font-weight:bold;
    }
//name selector
    .redFont 
    {
      color:#ff0000;
    }
  </fx:Style>
  <s:VGroup>
    <s:Label text="Hello World" styleName="redFont"/>
    <s:Button label="Click me"/> 
  </s:VGroup>
  <s:HGroup>
    <s:Label text="Hello World"/>
    <s:Button label="Click me"/> 
  </s:HGroup>      
</s:Application>
 
运行结果为:

==========================================    

    如果将style保存为css文件:

//ExternalStyles.css
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
 
global {
  font-family:Times New Roman, Times, serif;
  color:purple;
}
s|Label
{
  font-size:18;
  font-style:italic;
}
s|Button s|Label
{
  font-style:normal;
  font-weight:bold;
}
.redFont 
{
  color:#ff0000;
}
 
再修改mxml文件为:
 
//ExternalStyles.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" >
  <s:layout>
    <s:VerticalLayout horizontalAlign="center" paddingTop="20"/>
  </s:layout>
  <fx:Style source="ExternalStyles.css"/>
  <s:VGroup>
    <s:Label text="Hello World" styleName="redFont"/>
    <s:Button label="Click me"/> 
  </s:VGroup>
  <s:HGroup>
    <s:Label text="Hello World"/>
    <s:Button label="Click me"/> 
  </s:HGroup>      
</s:Application>
 
则运行结果一样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值