多重 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>
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 中使用