android中的樣式和CSS樣式作用相似,都是用于為界面元素定義顯示風格,它是一個包含一個或者多個view控件屬性的集合。如:需要定義字體的顏色和大小。   
    在CSS中是這樣定義的:
   
1 <style>
2     .itcast{COLOR:#0000CC;font-size:18px;}
3 </style>
可以像這樣使用上面的css樣式:

1 <div class="itcast">傳智播客</div>
在Android中可以這樣定義樣式:
在res/values/styles.xml文件中添加以下內容

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <style name=“itcast”> <!-- 為樣式定義一個全局唯一的名字-->
4         <item name="android:textSize">18px</item> <!-- name屬性為樣式要用在的View控件持有的屬性 -->
5         <item name="android:textColor">#0000CC</item>
6     </style>
7 </resources>
在layout文件中可以像下面這樣使用上面的android樣式:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" .>
3     <TextView style="@style/itcast"
4         ..  />
5 </LinearLayout>



<style>元素中有一個parent屬性。這個屬性可以讓當前樣式繼承一個父樣式,當前樣式可以繼承到父樣式的值。當然,如果父樣式的值不符合你的需求,你也可以對它進行修改,如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3     <style name="itcast">
 4         <item name="android:textSize">18px</item> <!-- name屬性為樣式要用在的View控件持有的屬性 -->
 5         <item name="android:textColor">#0000CC</item>
 6     </style>
 7     <style name="subitcast" parent="@style/itcast">
 8         <item name="android:textColor">#FF0000</item>
 9     </style>
10 </resources>


android中主題也是用于為應用定義顯示風格,它的定義和樣式的定義相同,如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <style name=“itcastTheme">
4     <item name=“android:windowNoTitle”>true</item> <!-- 沒標題 -->
5     <item name=“android:windowFullscreen”>?android:windowNoTitle</item> <!--全屏顯示-->
6 </style>
7 </resources>
上面“?android:windowNoTitle”中的問號用于引用在當前主題中定義過的資源的值。


下面代碼顯示在AndroidManifest.xml中如何為應用設置上面定義的主題:

1 <application android:icon="@drawable/icon" android:label="@string/app_name"
2      android:theme="@style/itcastTheme">
3    
4 </application>
除了可以在AndroidManifest.xml中設置主題,同樣也可以在代碼中設置主題,如下:
setTheme(R.style.itcastTheme);

盡管在定義上,樣式和主題基本相同,但是它們使用的地方不同。
    樣式用在單獨的View,如:EditText、TextView等;
    主題通過AndroidManifest.xml中的<application>和<activity>用在整個應用或者某個 Activity,主題對整個應用或某個Activity存在全局性影響。
    如果一個應用使用了主題,同時應用下的view也使用了樣式,那么當主題與樣式屬性發生沖突時,樣式的優先級高于主題。
    另外android系統也定義了一些主題,例如:<activity android:theme=“@android:style/Theme.Dialog”>,該主題可以讓Activity看起來像一個對話框,如果需要查閱這些主題,可以在文檔的reference-->android-->R.style 中查看。