在在视图显示中使用Style中,使用的是针对一个视图指定元素的样式。如果要针对整个Activity,对它的背景颜色和字体等做统一的样式约定,就需要使用另外一个技术,theme。
以下示例就是在在视图显示中使用Style基础上增加了theme。
首先,要编写theme文件,和style文件类似,是放在res/values目录下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="custom_background_color">#FFFFFFFF</color>
<style name="RiverTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_background_color</item>
</style>
</resources>
这里继承了系统的theme.light,一般theme是继承的,这样可以对默认的风格不必重复定义。
本例定义了一个背景色。这里背景色要单独声明,不能在item元素中直接写颜色值,会提示语法错误。
引用该theme,在manifest文件中指定的Activity中:
<activity android:name=".ListViewActivity" android:label="@string/app_name"
android:theme="@style/RiverTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
源代码见:
http://easymorse.googlecode.com/svn/tags/CustomListViewDemo-0.3/