標籤:
最近準備系統學一下android開發,這裡不會照搬原文,只會針對教程中一些難以理解的部分進行解釋,我只是個菜鳥。
在教程第一章——入門基礎裡面,講解android主題的那個小節,大概會有以下的代碼:
<!-- 位置 res/values/themes.xml --><?xml version="1.0" encoding="utf-8"?> <resources> <!-- 用於程式或活動的主題 --> <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar"> <!-- api_7+ --> <item name="actionBarStyle">@style/MyActionBar</item> <!-- api_11+ --> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> <!-- api_7+ --> <item name="background">@drawable/actionbar_background</item> <!-- api_11+ --> <item name="android:background">@drawable/actionbar_background</item> </style></resources>
這段代碼用於定義自己的風格:改變ActionBar的背景,這裡的問題在於api-7與api-11的代碼寫在同一個檔案裡面了,結果無法通過eclipse編譯,解決方案是將這個themes.xml分為多個檔案,工程自動產生的有values-v7、values-v11、values-v14,可以將themes.xml分為幾個不同的版本分別放到不同的values裡去,不同裝置的不同風格大概也是這樣實現的吧?
stackoverflow上的原文:
EDIT : If you want to be able to style your ActionBar to look the same in all API levels, you need to create different folders for the selected api level and create new style.xml / themes.xml files in these folders. For example :
- res -- values -- styles.xml -- themes.xml // API LEVEL 8+ -- values-v11 -- styles.xml -- themes.xml // API LEVEL 11+ -- values-v14 -- styles.xml -- themes.xml // API LEVEL 14+
在教程中沒有專門說明要添加資源:@drawable/actionbar_background,這裡只要添加一個1x1像素的圖片即可,background的預設效果是展開。
完
android官方開發教程解釋(一)