Android自带的一些可用于Activity的Theme

来源:互联网 发布:python api文档在哪有 编辑:程序博客网 时间:2024/05/02 06:08

http://kurtchen.com/blog/2010/03/10/android-theme/

在 AndroidMenifest.xml 中定义 Activity 的时候我们可以使用 android:theme 来设置 Activity 的主题,比如:

?
1
2
3
<activityandroid: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’)

当然,我们也可以自己创建Theme,比如:
colors.xml 
?
1
2
3
4
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<drawablename="green_background">#ff00ff00</drawable>
</resources>

theme.xml

?
1
2
3
4
5
6
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stylename="MyTheme">
<itemname="android:windowBackground">@drawable/green_background</item>
</style>
</resources>

Activity

?
1
2
3
<activityandroid: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
<stylename="MyDialog">
<itemname="android:windowBackground">@android:color/transparent</item>
<itemname="android:windowTitleStyle">@style/DialogWindowTitle</item>
<itemname="android:windowIsFloating">true</item>
<itemname="android:windowContentOverlay">@null</item>
</style>
<stylename="DialogWindowTitle">
<itemname="android:maxLines">1</item>
<itemname="android:scrollHorizontally">true</item>
 
<itemname="android:textAppearance">
@android:style/TextAppearance.DialogWindowTitle
</item>
 
</style>