根据网络上的说法,安卓UI发生两次大的改动。一次是android 3.0发布,UI开发支持了Fragment,主要增加了大屏幕显示的支持,这个版本就开始支持Holo Theme,由于android 3.X的设备占有率也不高,这一次的改变没有引起大的关注;再一次的改变就是Android 4.0,也就是通常所说的ICS(Ice Cream Sandwich),这个于2011年底发布的Android系统,同时也发布了指导性的应用设计规范《Android Design》有了设计规范的指导,就有了更多应用采用了Holo Theme,尤其国外的应用。Holo Theme的主要特点是轻快的颜色、适当的阴影、卡片化布局、方角矩形。
主题和风格的自定义都放在res/values/styles.xml文件中。xml文件部分如下:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<--主题-->
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<--风格-->
<style name="progress_wait" parent="@android :style/Widget.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">true</item>
<item name="android:indeterminateDrawable">@anim/progress_wait</item>
</style>
</resource>
一 主题(Theme)
A theme is a style applied to an entire Activity or application, rather than an individual View. 最简单浅显的比喻就是,你在使用Android Studio的时候,有好几个主题可以选,其中一个是Windows,一个是Intellij,调整后整个界面的主题就变了。这里的主题,类似于安卓里面的主题。
例如,在用Eclipse新建安卓工程的时候,主题选项如下图,目前安卓的风格也就如下几种:
图一 Eclipse新建工程师供选择的主题
主题效果如下(图片来自网络),从左到有的效果,分别是:Dark,Light,Light with Dark Action Bar:
图二 各种风格效果图
主题放置的位置在<application>标签或者是<activity>标签,如:
<application android:theme="@style/AppTheme"></application>
<activity android:theme="@style/AppTheme"></activity>
二 风格(Style)
参见官方文档。鄙人稍作翻译。
A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.
风格就是用于指定视图或窗口的风格和格式的集合。风格可以指定的属性有如下,但不限于以下,高度,内间距,字体颜色,字体大小,背景色,等等。一组风格定义在xml文件中,不同于用于布局的xml文件。
Styles in Android share a similar philosophy to cascading stylesheets in web design—they allow you to separate the design from the content.
安卓中的风格类似于CSS的共用思想:设计和内容分离。
For example, by using a style, you can take this layout XML:(你可以通过如下方式使用风格)
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="@string/hello" />
And turn it into this:(也可以转变成如下)
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
All of the attributes related to style have been removed from the layout XML and put into a style definition called CodeFont, which is then applied with the style attribute.
所有与风格相关的属性全部从布局文件中移除,定义到风格资源文件写,并冠名CodeFont,这就是我们稍后要应用到的风格属性。