flex4.5中CSS選擇器的應用小結
更新時間:2013年04月28日 16:40:03 作者:
與HTML相似,Flex允許在MXML標簽中通過CSS樣式來設置組件的外觀。到flex4.5后已經基本上支持了HTML中的所有CSS的應用方式,這里主要來列舉下flex4.5中CSS選擇器的使用方法
CSS選擇器可以包括,標簽選擇器、類別選擇器、ID選擇器、交集選擇器、并集選擇器、后代選擇器、全局選擇器、偽類等,這些樣式應用都已經在flex得到支持
1.標簽選擇器
標簽選擇器是根據MXML文件中組件的類型來設置的,示例如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|TextInput{
color: #FF0000;
}
s|Button{
color: #FFFF00;
}
</fx:Style>
<s:TextInput text="text input"/>
<s:Button label="button"/>
上面二個控件的顏色會隨之改變。
2.類別選擇器
類別選擇器是以一個點開頭,后面加上組件中通過styleName設置的樣式名來表示的類別選擇器,示例如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.btn2{
color: #FF0000;
}
</fx:Style>
<s:Button label="button2" styleName="btn2"/>
3.ID選擇器
ID選擇器是以#開頭,后面加上組件中設置的ID名來表示的類別選擇器,示例如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
#btn1{
color: #FF0000;
}
.btn2{
color: #FF00FF;
}
</fx:Style>
<s:Button id="btn1" label="button1"/>
<s:Button label="button2" styleName="btn2"/>
4.交集選擇器
交集選擇器由兩個選擇器直接連接構成,其結果是選中二者各自元素范圍的交集,示例如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|Button#btn1{
color: #FF0000;
}
s|Button.btn2{
color: #FF00FF;
}
</fx:Style>
<s:Button id="btn1" label="button1"/>
<s:Button label="button2" styleName="btn2"/>
<s:Button label="button3"/>
5.并集選擇器
并集選擇器是多個選擇器通過逗號連接而成的,并集選擇器同時選中各個基本選擇器所選擇的范圍,任何形式的選擇器都可以,示例如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|Button#btn1,s|Button.btn2{
color: #FF0000;
}
</fx:Style>
<s:Button id="btn1" label="button1"/>
<s:Button label="button2" styleName="btn2"/>
<s:Button label="button3"/>
6.后代選擇器
后代選擇器也叫派生選擇器,可以使用后代選擇器給一個元素里的子元素定義樣式,示例如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|HGroup s|TextInput{
color: #FF0000;
}
</fx:Style>
<s:HGroup verticalAlign="middle">
<s:Label text="Text Input 1"/>
<s:TextInput text="sample"/>
</s:HGroup>
<s:TextInput text="sample"/>
7.全局選擇器
全局選擇器global可以將樣式應用到所有的組件,示例如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
global{
color: #FF0000;
}
</fx:Style>
<s:Label text="label"/>
<s:TextInput text="text input"/>
<s:Button label="button"/>
8.偽類
偽類是用來設置組件在不同狀態(tài)下的樣式,示例如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|Button:up{
color: #FF0000;
}
s|Button:down{
color: #FF00FF;
}
s|Button:over{
color: #0000FF;
}
</fx:Style>
<s:Button label="button"/>
1.標簽選擇器
標簽選擇器是根據MXML文件中組件的類型來設置的,示例如下:
復制代碼 代碼如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|TextInput{
color: #FF0000;
}
s|Button{
color: #FFFF00;
}
</fx:Style>
<s:TextInput text="text input"/>
<s:Button label="button"/>
上面二個控件的顏色會隨之改變。
2.類別選擇器
類別選擇器是以一個點開頭,后面加上組件中通過styleName設置的樣式名來表示的類別選擇器,示例如下:
復制代碼 代碼如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.btn2{
color: #FF0000;
}
</fx:Style>
<s:Button label="button2" styleName="btn2"/>
3.ID選擇器
ID選擇器是以#開頭,后面加上組件中設置的ID名來表示的類別選擇器,示例如下:
復制代碼 代碼如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
#btn1{
color: #FF0000;
}
.btn2{
color: #FF00FF;
}
</fx:Style>
<s:Button id="btn1" label="button1"/>
<s:Button label="button2" styleName="btn2"/>
4.交集選擇器
交集選擇器由兩個選擇器直接連接構成,其結果是選中二者各自元素范圍的交集,示例如下:
復制代碼 代碼如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|Button#btn1{
color: #FF0000;
}
s|Button.btn2{
color: #FF00FF;
}
</fx:Style>
<s:Button id="btn1" label="button1"/>
<s:Button label="button2" styleName="btn2"/>
<s:Button label="button3"/>
5.并集選擇器
并集選擇器是多個選擇器通過逗號連接而成的,并集選擇器同時選中各個基本選擇器所選擇的范圍,任何形式的選擇器都可以,示例如下:
復制代碼 代碼如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|Button#btn1,s|Button.btn2{
color: #FF0000;
}
</fx:Style>
<s:Button id="btn1" label="button1"/>
<s:Button label="button2" styleName="btn2"/>
<s:Button label="button3"/>
6.后代選擇器
后代選擇器也叫派生選擇器,可以使用后代選擇器給一個元素里的子元素定義樣式,示例如下:
復制代碼 代碼如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|HGroup s|TextInput{
color: #FF0000;
}
</fx:Style>
<s:HGroup verticalAlign="middle">
<s:Label text="Text Input 1"/>
<s:TextInput text="sample"/>
</s:HGroup>
<s:TextInput text="sample"/>
7.全局選擇器
全局選擇器global可以將樣式應用到所有的組件,示例如下:
復制代碼 代碼如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
global{
color: #FF0000;
}
</fx:Style>
<s:Label text="label"/>
<s:TextInput text="text input"/>
<s:Button label="button"/>
8.偽類
偽類是用來設置組件在不同狀態(tài)下的樣式,示例如下:
復制代碼 代碼如下:
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
s|Button:up{
color: #FF0000;
}
s|Button:down{
color: #FF00FF;
}
s|Button:over{
color: #0000FF;
}
</fx:Style>
<s:Button label="button"/>
相關文章
Flex中如何動態(tài)生成DataGrid以及動態(tài)生成表頭
因某些需要,DataGrid及其表頭需要動態(tài)生成,網上的解決方案打多籠統(tǒng),下面有個不錯的解決方法,感興趣的朋友可以參考下2013-10-10
Flex 錯誤(mx.messaging.messages::RemotingMessage)分析
有時我們在做項目的時候會遇到Flex 錯誤提示mx.messaging.messages::RemotingMessage,那么產生這個錯誤的原因是什么呢,今天我們來分析下2014-06-06
Flex中的HDividedBox和VDividedBox的比較附圖
學習Flex的朋友對HDividedBox和VDividedBox并不陌生吧,下面是兩者的簡單比較,感興趣的朋友可以參考下2013-10-10
Flex中TabNavigator設置Tabs樣式思路及源碼
這篇文章主要介紹了Flex中TabNavigator如何設置Tabs樣式有哪些思路,感興趣的朋友可以看看下面的源碼2014-05-05

