轉載請註明出處:http://blog.csdn.net/asdzheng/article/details/42838279
本文來源於Android Doc,英文好的也可以直接看原文,原文地址:http://developer.android.com/guide/topics/ui/themes.html
如有發現翻譯錯誤,請留言指出,謝謝。另外,本文的鏈接大部分Android Doc在國內的鏡像,不用翻牆也能訪問。
樣式與佈局
Style 是Window和View的外觀和格式的屬性集合。它可以作爲高度,間距,字體大小,背景顏色等屬性。Style是一種 xml 資源文件,放在和佈局文件不同的文件夾裏;
Style 的設計理念和 Web一脈相承——即它們都允許你把內容和樣式分離。
例如,你用Style可以把下面這個xml文件:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="@string/hello" />
替換成下面這種形式:
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
看上面的例子,佈局裏的全部屬性都放到一個名爲CodeFont的Style裏,這裏的CodeFont就是一個Style。你在接下來的內容裏會看到這個佈局是怎麼被定義的。
你可以把Theme當成是整個Activity或者整個程序的樣式(Style),它的覆蓋範圍比上面例子裏那個view的範圍更大。當一個Style被當成一個Theme時,Activity或整個程序裏的每一個View都會用到同樣style屬性,如果那些屬性它們支持的話。舉個例子,你可以把一個Activity的樣式設置成上面CodeFont,這樣的話Activity裏面的所有字體就都會成綠色的“monospace”了。
定義Style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
繼承
<style name="GreenText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
如果你想要繼承那些你自己定義的style,你不一定需要用到parent這個屬性。你可以用另一種方式來代替,把你想要繼承的那個style作爲前綴,用一點與你想要新建的style分隔開來。例如,創建一個繼承自上面例子裏的CodeFont 樣式,但把顏色改成紅的,你可以像下面這樣創建一個新的style:<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
在看上面的例子裏,在style裏並沒有parent屬性,只是在name屬性裏把CodeFont作爲前綴放在Red的前面,這樣也能繼承CodeFont裏面的所有屬性。這個style複寫了textColor屬性,將字體顏色變成紅的。這樣定義以後,你就可以用@style/CodeFont.Red 這種方式來引用這個新的style了。 <style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
上面這種寫法的style就是繼承自CodeFont 和 CodeFont.Red,然後加上android:textSize 屬性。樣式屬性
<EditText
android:inputType="number"
... />
你也可以創建一個style作爲EditText的屬性替代上面那種方式:<style name="Numbers">
<item name="android:inputType">number</item>
...
</style>
這樣你的EditText就可以用這個style了:<EditText
style="@style/Numbers"
... />
這個簡單的例子可能會讓你覺得用了style反而更麻煩,其實未必,當在一個真實的項目裏,你把很多需要複用到的屬性寫到一個style裏,供所有用到的那些屬性的地方使用,那時你就會覺得節省裏很多麻煩。還有一些style屬性,只支持被應用到主題theme裏,而不支持用在單個View裏。這些style屬性只支持運用到整個窗口而不支持單個View。例如,一些style屬性作爲作爲主題可以隱藏掉程序的標題、狀態欄,或者改變整個窗口的背景色。這些屬性並不屬於任何一個視圖View類。如果你想要知道有哪些style屬性屬於窗口特有的,你可以參考R.attr這個鏈接裏開頭的內容。例如,windowNoTitle和windowBackground這兩個屬性只在style應用在Activity或者整個程序裏纔有用。下一節內容就是關於style是怎麼作爲主題應用到Activity或程序裏的。
在UI上設置Style和Theme
在View裏面設置style
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
現在CodeFont裏的屬性將被設置進TextView裏。注意:設置style屬性時不用加上android: 這個命名空間。在Activity或application裏設置主題
<application android:theme="@style/CustomTheme">
如果你想要設置一個activity的主題,只要把android:theme屬性加到<activity>標籤裏就好。<activity android:theme="@android:style/Theme.Dialog">
<activity android:theme="@android:style/Theme.Translucent">
如果你喜歡一個主題,但想調整一些屬性,你可以新建一個主題然後用parent屬性去繼承它。例如,你可以加上自己的顏色在普通的 light 主題上面:<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>
(請注意,上面例子裏的android:windowBackground屬性值只支持已經定義好了的顏色引用,不像android:colorBackground屬性值還可以直接用顏色的數值)<activity android:theme="@style/CustomTheme">
選擇一個基於平臺版本的主題
<style name="LightThemeSelector" parent="android:Theme.Light">
...
</style>
<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
...
</style>
現在你可以使用任何主題像這個例子一樣,當你的程序運行在Android 3.0 或以上,你的程序將自動轉換到holographic 系列主題。