在AndroidManifest.xml中,可以定義整個app的主題,也可以定義單個activity的主題。下面的樣本分別給application和activity定義了一個主題:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" <application android:theme="custom_application_theme" <activity android:name=".Activity"android:theme="custom_activity_theme"/> </application></manifest>
接下來的問題就是定義自己的主題了,事實上,主題(theme)只不過是一個作用範圍更大的樣式(style)集,所以定義theme和定義style的格式是一樣的。如果使用eclipse建立一個4.0以上的空白工程,可以看到如下的AndroidManifest.xml:
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.yg.xesam.net.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
查看@style/AppTheme的內容可以看到最終的樣式定義(系統的themes檔案放在sdk對應平台的/res/values/themes.xml中)。
自訂theme可以定義在styles.xml中,但是為了方便,也可以單獨定義在自己建立的themes.xml中:
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="custom_application_theme" parent="android:Theme.Light"></style> <style name="custom_activity_theme" parent="custom_application_theme"></style></resources>
具體內容可以參考sdk內建的themes.xml檔案,比如在繼承parent(android:Theme.Light)樣式的基礎上再定義程式全螢幕,是否有標題列等等。
下面的例子中:
custom_application_theme定義沒有標題列並且全屏,custom_activity_theme覆蓋了custom_application_theme的android:windowNoTitle屬性,所以custom_activity_theme是全屏但是保留標題列
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="custom_application_theme" parent="android:Theme.Light"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> </style> <style name="custom_activity_theme" parent="custom_application_theme"> <item name="android:windowNoTitle">false</item> </style></resources>