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
>
|