http://kurtchen.com/blog/2010/03/10/android-theme/
在 AndroidMenifest.xml 中定義 Activity 的時候我們可以使用 android:theme 來設置 Activity 的主題,比如:
1
2
3
|
< activity
android:name = ".MainActivity" android:label = "@string/app_name" android:theme = "@android:style/Theme.NoTitleBar" > |
Android 本身自帶了一些 Theme ,可以在frameworks/base/core/res/res/values/themes.xml中找到,從這個文件中也能看到默認的Theme是如何定義的(Theme)。
Theme
Theme.NoTitleBar
Theme.NoTitleBar.Fullscreen
Theme.Light
Theme.Light.NoTitleBar
Theme.Light.NoTitleBar.Fullscreen
Theme.Black
Theme.Black.NoTitleBar
Theme.Black.NoTitleBar.Fullscreen
Theme.Wallpaper
Theme.Wallpaper.NoTitleBar
Theme.Wallpaper.NoTitleBar.Fullscreen
Theme.WallpaperSettings
Theme.Light.WallpaperSettings
Theme.Translucent
Theme.Translucent.NoTitleBar
Theme.Translucent.NoTitleBar.Fullscreen
Theme.Dialog
Theme.Panel
Theme.Light.Panel
Theme.InputMethod
其中有一個比較有意思的是 Theme.NoDisplay :
Default theme for activities that don’t actually display a UI; that is, they finish themselves before being resumed.
還有幾個不知道爲什麼是不能使用的(#TODO),比如 Theme.Dialog.Alert ,會報錯:
Error: Resource is not public. (at ‘theme’ with value ‘@android:style/Theme.Dialog.Alert’)
colors.xml
1
2
3
4
|
<? xml
version = "1.0"
encoding = "utf-8" ?> < resources > < drawable
name = "green_background" >#ff00ff00</ drawable > </ resources > |
theme.xml
1
2
3
4
5
6
|
<? xml
version = "1.0"
encoding = "utf-8" ?> < resources > < style
name = "MyTheme" > < item
name = "android:windowBackground" >@drawable/green_background</ item > </ style > </ resources > |
Activity
1
2
3
|
< activity
android:name = ".MainActivity" android:label = "@string/app_name" android:theme = "@style/MyTheme" > |
再舉個例子,比如在 theme.xml 中 Theme.Dialog.Alert 不給我們用,我們可以自己定義一份拿來使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< style
name = "MyDialog" > < item
name = "android:windowBackground" >@android:color/transparent</ item > < item
name = "android:windowTitleStyle" >@style/DialogWindowTitle</ item > < item
name = "android:windowIsFloating" >true</ item > < item
name = "android:windowContentOverlay" >@null</ item > </ style > < style
name = "DialogWindowTitle" > < item
name = "android:maxLines" >1</ item > < item
name = "android:scrollHorizontally" >true</ item > < item
name = "android:textAppearance" > @android:style/TextAppearance.DialogWindowTitle </ item > </ style > |