我為 style 和 theme 而狂(3)

impossible-shapes-orig.jpg
多重 style
理論上是無法再同一個檢視元件應用多個 style,但是就一種列外就是 TextAppearance 作為檢視(這裡可能不準確)允許定義一個 style 。
TextAppearance 屬性
- textColor
- textColorHighlight
- textColorHint
- textColorLink
- textSize
- textStyle
- fontFamily
- typeface
- ...
<TextView style="@style/MyStyle" android:textAppearance="@style/MyStyle"/>
這裡一個 style 定義文字的外觀,另一個用於定義 style 文字以外的外觀。
我們定義一個 TextAppearance 的 style,通常會繼承 TextAppearance.AppCompact 然後在其中修改一個 TextAppearance 的屬性。
<style name="MyText" parent="TextAppearance.AppCompat"> <item name="android:textColor">#F000</item> </style>

amusement-park-9.jpg
Theme
相對於區域性的 style,theme 是全域性的,style 從檢視相同樣式提取的,主題著放眼全域性。我們在 theme 定義一個屬性,這個屬性將作用於整個我們 Android 應用。可以在 theme 為我們所有 widget 和視窗設定預設值。在可以 Activity 層面上進行主題化。也可以設定系統級別上建立的檢視。我們也可以自由更換主題。
Themes VS Styles
- 其實並沒有什麼區別
<style name="Style"> <item name="android:background">#FF0000</item> </style> <style name="Theme"> <item name="android:statusBarColor">@color/blue1</item> </style>
通過上面 Style 和 Theme 定義來看並沒有什麼區別,在其中定義一個屬性。
- 不同的作用域
兩者的作用域是不同,這個值得注意一下。
- theme 是視窗級別的要是 manifest 使用
- style 是視窗元素級別 佈局 xml 中使用