最近突然想要自己开发一个主题,查了些资料,想从最基本的开始入手,于是有了想法把Style 和 Theme相关的文章翻译出来, 让自己掌握的更好一些。
我找的资料是 SDK下的相关文档。
Style:A style is a collection of properties that specify the look and format for a View or window.
Style是为View或窗口让它显示指定的外观或格式的属性的集合
A style is defined in an XML resource that is separate from the XML that specifies the layout.[/color]
Style定义在XML资源文件中并且从别的XML文件分隔出来,用来定义指定的布局
例如:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="@string/hello" />
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
[color=blue]
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.
关于Style相关的属性已经从布局XML文件汇总提取出来并且放到了CodeFont里面,通过它可以使用已经定义好的属性。
Theme:A theme is a style applied to an entire Activity or application, rather than an individual View (as in the example above).
[/color]
Theme是一种Style应用于整个Activity或Application, 而不是在一个VIew中(就像上面提到的那样)
When a style is applied as a theme, every View in the Activity or application will apply each style property that it supports. [color=blue]
当一个Style使用成Theme的时候,在Acitivity或Application里面的任何一个View,会使用它所支持的每一个Style属性
The parent attribute in the <style> element is optional and specifies the resource ID of another style from which this style should inherit properties. You can then override the inherited style properties if you want to.[/color]
<style>里面的父属性 是一个可选的它可以指定一个资源ID表明应该从哪里开始继承style属性,然后你可以覆盖该属性。
If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period. For example, to create a new style that inherits the CodeFont style defined above, but make the color red, you can author the new style like this:
当你希望从自己定义的style继承属性的时候, 不一定非要使用parent这个属性。相反,你只需要加上一个前缀代表你要继承的style风格的名称,用句号隔开
<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
当然这种继承你可以重复的写
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
这样多重的方式继承只适用于自己已经定义好的style, 在android固有的style是无法使用这种方法继承的,只能使用parent
[color=red]Applying Styles and Themes to the UI
* To an individual View, by adding the style attribute to a View element in the XML for your layout.
* Or, to an entire Activity or application, by adding the android:theme attribute to the <activity> or <application> element in the Android manifest.
When you apply a style to a single View in the layout, the properties defined by the style are applied only to that View. If a style is applied to a ViewGroup, the child View elements will not inherit the style properties—only the element to which you directly apply the style will apply its properties.[/color]
当你使用一个style到一个单一的view的时候,定义在style中的属性只会使用在当前view上
如果一个style使用到ViewGroup, 子元素不会继承该属性,只有直接继承的属性继承
[color=blue]To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. [/size]
Apply a style to a View[size=x-large]
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
Apply a theme to an Activity or application[/size]
[size=medium]<application android:theme="@style/CustomTheme">
If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the <activity> tag instead.[/color]
<activity android:theme="@android:style/Theme.Dialog">
<activity android:theme="@android:style/Theme.Translucent">[color=indigo]
如果想要了解更多的系统已经定义好的style属性,请查看R.style类
但是因为R.style里面描述的不是很详细,如果想要学习更多的知识及
使用方法的话请查看源代码,其中
* Android Styles (styles.xml)
* Android Themes (themes.xml)
在这两个文件会帮助你学到每个主题及风格的具体实现