【翻译】(22)样式与主题
see
http://developer.android.com/guide/topics/ui/themes.html
原文见
http://developer.android.com/guide/topics/ui/themes.html
-------------------------------
Styles and Themes
样式与主题
-------------------------------
In this document
本文目录
* Defining Styles 定义样式
* Inheritance 继承
* Style Properties 样式属性
* Applying Styles and Themes to the UI 应用样式和主题到用户界面
* Apply a style to a View 应用样式到一个View
* Apply a theme to an Activity or application 应用一个主题到一个Activity或应用程序
* Select a theme based on platform version 基于平台版本选择一个主题
* Using Platform Styles and Themes 使用平台样式和主题
See also
另见
Style and Theme Resources 样式与主题资源
R.style for Android styles and themes 用于Android样式和主题的R.style
R.attr for all style attributes 用于所有样式属性的R.attr
-------------------------------
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.
样式是一个属性集合,它指定一个View或窗口的外观和格式。一个样式可以指定属性诸如高度,内边距,字体颜色,字体大小,背景颜色,以及更多。一个样式被定义在一个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.
在Android中样式共享一个类似于网页开发中层叠样式表的哲学——它们允许你从内容中分离设计。
For example, by using a style, you can take this layout XML:
例如,通过使用一个样式,你可以把这个布局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. You'll see the definition for this style in the following section.
所有与样式相关的属性被移除出布局XML,并且放进一个称为CodeFont的样式定义,然后它被用style属性来应用。你将在以下章节中看到这个样式的定义。
A theme is a style applied to an entire Activity or application, rather than an individual View (as in the example above). When a style is applied as a theme, every View in the Activity or application will apply each style property that it supports. For example, you can apply the same CodeFont style as a theme for an Activity and then all text inside that Activity will have green monospace font.
一个主题是一个应用到整个Activity或应用程序的样式,而非一个单独的View(如上面的示例中那样)。当一个样式被应用为一个主题时,Activity或应用程序中任一View将应用它支持的每个风格属性。例如,你可以应用相同的CodeFont样式作为一个Activity的主题,然后在那个Activity内的所有文本将拥有绿色等宽字体。
-------------------------------
Defining Styles
定义样式
To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder.
为了创建一组样式,保存一个XML文件在你的工程的res/values/目录下。XML文件的名称是任意的,但它必须使用.xml扩展名并且保存在res/values/文件夹中。
The root node of the XML file must be <resources>.
XML文件的根节点必须是<resources>。
For each style you want to create, add a <style> element to the file with a name that uniquely identifies the style (this attribute is required). Then add an <item> element for each property of that style, with a name that declares the style property and a value to go with it (this attribute is required). The value for the <item> can be a keyword string, a hex color, a reference to another resource type, or other value depending on the style property. Here's an example file with a single style:
对于你想创建的每个样式,添加一个<style>元素到文件,带有一个唯一标识样式的名称(这个属性是必需的)。然后为那个样式的每个属性添加一个<item>元素,带有一个声明样式属性的名称和伴随它的值(这个属性是必需的)。<item>的值可以是关键词字符串,十六进制颜色,另一个资源类型的引用,或其它依赖于样式属性的值。这里有一个带有单一样式的示例文件:
-------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
-------------------------------
Each child of the <resources> element is converted into an application resource object at compile-time, which can be referenced by the value in the <style> element's name attribute. This example style can be referenced from an XML layout as @style/CodeFont (as demonstrated in the introduction above).
<resources>元素的每个子节点在编译期被转换为一个应用程序资源对象,它可以被<style>元素的name属性的值引用。这个示例样式可以从一个XML布局中被引用为@style/CodeFont(正如上面的介绍中所演示的那样)。
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.
<style>元素中的parent属性是可选的并且指定其它样式的资源ID,而这个样式应该从它那里继承属性。然后你可以覆盖继承的样式属性如果你希望的话。
Remember, a style that you want to use as an Activity or application theme is defined in XML exactly the same as a style for a View. A style such as the one defined above can be applied as a style for a single View or as a theme for an entire Activity or application. How to apply a style for a single View or as an application theme is discussed later.
记住,一个你希望使用作为一个Activity或应用程序主题的样式被定义在XML中,与作为一个View的样式完全相同。一个样式诸如上面定义的那个可以被应用为用于单一View的样式或作为用于整个Activity或应用程序的主题。稍后讨论如何应用样式到一个单一View或作为一个应用程序的主题。
Inheritance
继承
The parent attribute in the <style> element lets you specify a style from which your style should inherit properties. You can use this to inherit properties from an existing style and then define only the properties that you want to change or add. You can inherit from styles that you've created yourself or from styles that are built into the platform. (See Using Platform Styles and Themes, below, for information about inheriting from styles defined by the Android platform.) For example, you can inherit the Android platform's default text appearance and then modify it:
<style>元素的parent属性让你指定一个样式,你的样式应该从这个样式中继承属性。你可以使用它从一个现存样式中继承属性,然后只定义你希望改变或添加的属性。你可以从你曾经自己创建的样式或从内建进平台的样式中继承。(参见下面的使用平台样式和主题章节,获取关于从Android平台定义的样式中继承的信息。)例如,你可以继承Android平台的默认文本外观然后修改它:
-------------------------------
<style name="GreenText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</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:
如果你希望从你自己定义的的样式中继承,那么你不必使用parent属性。取而代之的是,只要把你希望继承的样式名称作为你的新样式名称的前缀,通过一个句号来分隔。例如,为了创建一个继承上面定义的CodeFont样式的新样式,但把颜色变成红色,你可以像这样创作新的样式:
-------------------------------
<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
-------------------------------
Notice that there is no parent attribute in the <style> tag, but because the name attribute begins with the CodeFont style name (which is a style that you have created), this style inherits all style properties from that style. This style then overrides the android:textColor property to make the text red. You can reference this new style as @style/CodeFont.Red.
注意在<style>标签中没有parent属性,但因为name属性以CodeFont样式名称开头(它是你曾经创建的一个样式),这个样式从那个属性中继承所有样式属性。然后这个样式覆盖android:textColor属性使文本变红。你可以引用这个新的样式作为@style/CodeFont.Red。
You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend CodeFont.Red to be bigger, with:
你可以通过用句号链接名称,像这样继续继承如你喜欢的那样的多次。例如,你可以用这种方法扩展CodeFont.Red以变得更大:
-------------------------------
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
-------------------------------
This inherits from both CodeFont and CodeFont.Red styles, then adds the android:textSize property.
它同时继承CodeFont和CodeFont.Red风格,然后添加android:textSize属性。
-------------------------------
Note: This technique for inheritance by chaining together names only works for styles defined by your own resources. You can't inherit Android built-in styles this way. To reference a built-in style, such as TextAppearance, you must use the parent attribute.
注意:这种通过链接名称在一起来继承的技术只工作于被你自己的资源定义的样式。你不能用这种方式继承Android的内建样式。为了引用一个内建样式,诸如TextAppearance,你必须使用parent属性。
-------------------------------
Style Properties
样式属性
Now that you understand how a style is defined, you need to learn what kind of style properties—defined by the <item> element—are available. You're probably familiar with some already, such as layout_width and textColor. Of course, there are many more style properties you can use.
现在你理解样式是如何被定义的,你需要知道有什么类型的样式属性——用<itme>元素定义——是可用的。你很可能已经熟悉一些,诸如layout_width和textColor。当然,有其它许多你可以使用的样式属性。
The best place to find properties that apply to a specific View is the corresponding class reference, which lists all of the supported XML attributes. For example, all of the attributes listed in the table of TextView XML attributes can be used in a style definition for a TextView element (or one of its subclasses). One of the attributes listed in the reference is android:inputType, so where you might normally place the android:inputType attribute in an <EditText> element, like this:
找寻被应用到一个特定View的属性的最佳地方是相应的类参考文档,它列举所有支持的XML属性。例如,在TextView XML属性表格中列举的所有属性可以用于TextView元素的样式定义中(或它的其中一个子类)。其中有一个列举在参考中的属性叫android:inputType,所以在你可能通常放置android:inputType属性在一个<EditText>元素内的地方,像这样:
-------------------------------
<EditText
android:inputType="number"
... />
-------------------------------
You can instead create a style for the EditText element that includes this property:
取而代之,你可以为包含这个属性的EditText元素创建一个样式。
-------------------------------
<style name="Numbers">
<item name="android:inputType">number</item>
...
</style>
-------------------------------
So your XML for the layout can now implement this style:
这样你的布局XML现在可以实现这个样式:
-------------------------------
<EditText
style="@style/Numbers"
... />
-------------------------------
This simple example may look like more work, but when you add more style properties and factor-in the ability to re-use the style in various places, the pay-off can be huge.
这个简单示例可能看起来比较好,但当你添加更多更多样式属性并提取功能因子以在不同的地方重用样式时,开销可能是巨大的。
For a reference of all available style properties, see the R.attr reference. Keep in mind that all View objects don't accept all the same style attributes, so you should normally refer to the specific View class for supported style properties. However, if you apply a style to a View that does not support all of the style properties, the View will apply only those properties that are supported and simply ignore the others.
想获取所有可用样式属性的参考文档,请参见R.attr参考文档。请牢记所有View对象不接受所有相同的样式属性,所以你通常应该对支持的样式属性引用特定的View类。然而,如果你应用一个样式到一个不支持所有样式属性的View,该View将只应用那些支持的属性并且简单地忽略掉其它属性。
Some style properties, however, are not supported by any View element and can only be applied as a theme. These style properties apply to the entire window and not to any type of View. For example, style properties for a theme can hide the application title, hide the status bar, or change the window's background. These kind of style properties do not belong to any View object. To discover these theme-only style properties, look at the R.attr reference for attributes that begin with window. For instance, windowNoTitle and windowBackground are style properties that are effective only when the style is applied as a theme to an Activity or application. See the next section for information about applying a style as a theme.
然而,一些样式属性不被任何View元素支持并且只可以被应用为一个主题。这些样式属性应用到整个窗口而不会应用到任何类型的View。例如,一个主题的样式属性可以隐藏应用程序标题,隐藏状态栏,或改变窗口的背景。这些类型的样式属性不属于任何View对象。为了发现这些只限于主题的样式属性,请查看R.attr关于window开头的属性的参考。例如,windowNoTitle和windowBackground是样式属性,它仅当样式被应用为一个Activity或应用程序的主题时才有效。参见下一章节以获取关于把样式应用为主题的信息。
-------------------------------
Note: Don't forget to prefix the property names in each <item> element with the android: namespace. For example: <item name="android:inputType">.
注意:不要忘记用android:名字空间前缀每个<item>元素的属性名称。例如:<item name="android:inputType">
-------------------------------
-------------------------------
Applying Styles and Themes to the UI
应用样式和主题到用户界面
There are two ways to set a style:
有两种方式设置一个样式:
* To an individual View, by adding the style attribute to a View element in the XML for your layout.
* 对于一个单独的View,通过添加style属性到你的布局XML中的一个View元素。
* Or, to an entire Activity or application, by adding the android:theme attribute to the <activity> or <application> element in the Android manifest.
* 或者,对于整个Activity或应用程序,通过在Android清单中添加android:theme属性到<activity>或<application>元素。
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. However, you can apply a style so that it applies to all View elements—by applying the style as a theme.
当你引用一个样式到布局内的单一View时,被样式定义的属性仅应用在那个View上。如果一个样式被应用到一个ViewGroup,那么子View元素将不会继承样式属性——只有你直接把样式应用到的元素才会应用它的属性。然而,你可以应用一个样式,使它应用到所有View元素——通过应用一个样式作为一个主题。
To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. When you do so, every View within the Activity or application will apply each property that it supports. For example, if you apply the CodeFont style from the previous examples to an Activity, then all View elements that support the text style properties will apply them. Any View that does not support the properties will ignore them. If a View supports only some of the properties, then it will apply only those properties.
为了把一个样式定义应用为一个主题,你必须在Android清单中把样式应用到一个Activity或应用程序。当你这样做时,Activity或应用程序内的每个View将应用它支持的每个属性。例如,如果你应用来自前面示例中的CodeFont样式到一个Activity,那么所有支持text样式属性的View元素将应用它们。任何不支持该属性的View将忽略它们。如果一个View只支持其中一些属性,那么它将只应用那些属性。
Apply a style to a View
应用一个样式到一个View
Here's how to set a style for a View in the XML layout:
这里是如何为XML布局内的View设置样式:
-------------------------------
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
-------------------------------
Now this TextView will be styled as defined by the style named CodeFont. (See the sample above, in Defining Styles.)
现在这个TextView将被样式化为称为CodeFont的样式所定义的那样。(见上面的示例,在定义样式章节中。)
-------------------------------
Note: The style attribute does not use the android: namespace prefix.
注意:style属性不使用android:名字空间前缀。
-------------------------------
Apply a theme to an Activity or application
应用一个主题到一个Activity或应用程序
To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example:
为了为你的应用程序的所有活动设置一个样式,打开AndroidManifest.xml文件并编辑<application>标签以包含带有样式名称的android:theme属性。例如:
-------------------------------
<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.
如果你希望一个主题在你的应用程序中应用到仅一个Activity上,那么改为添加android:theme属性到<activity>标签上。
Just as Android provides other built-in resources, there are many pre-defined themes that you can use, to avoid writing them yourself. For example, you can use the Dialog theme and make your Activity appear like a dialog box:
正如Android提供其他内建资源,有许多你可以使用的预定义主题,以避免自己书写它们。例如,你可以使用Dialog主题让你的Activity显得像一个对话框:
-------------------------------
<activity android:theme="@android:style/Theme.Dialog">
-------------------------------
Or if you want the background to be transparent, use the Translucent theme:
或者如果你希望背景是透明的,那么使用Translucent(注:半透明)主题:
-------------------------------
<activity android:theme="@android:style/Theme.Translucent">
-------------------------------
If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional light theme to use your own color like this:
如果你喜欢一个主题,但希望调整它,那么只要添加该主题作为你自定义主题的父主题。例如,你可以修改传统的浅色主题以使用你自己的颜色,像这样:
-------------------------------
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>
-------------------------------
(Note that the color needs to supplied as a separate resource here because the android:windowBackground attribute only supports a reference to another resource; unlike android:colorBackground, it can not be given a color literal.)
(注意这里的颜色需要提供以作为一个分离的资源,因为android:windowBackground属性只支持指向另一个资源的引用;不像android:colorBackground,它不能赋予一个颜色字面值。)
Now use CustomTheme instead of Theme.Light inside the Android Manifest:
注意在Android清单中使用CustomTheme而非Theme.Light:
-------------------------------
<activity android:theme="@style/CustomTheme">
-------------------------------
Select a theme based on platform version
基于平台版本选择一个主题
Newer versions of Android have additional themes available to applications, and you might want to use these while running on those platforms while still being compatible with older versions. You can accomplish this through a custom theme that uses resource selection to switch between different parent themes, based on the platform version.
较新版本的Android拥有对于应用程序可用的附加主题,而你可能希望在运行于这些平台上时使用这些主题但仍然兼容较旧的版本。你可以通过一个使用资源选择以切换不同的父主题的自定义主题来做到这点,基于平台版本。
For example, here is the declaration for a custom theme which is simply the standard platforms default light theme. It would go in an XML file under res/values (typically res/values/styles.xml):
例如,这里是用于自定义主题的声明,它简单地是标准平台的默认浅色主题。它将放进res/values下的一个XML文件(典型地是res/values/styles.xml):
-------------------------------
<style name="LightThemeSelector" parent="android:Theme.Light">
...
</style>
-------------------------------
To have this theme use the newer holographic theme when the application is running on Android 3.0 (API Level 11) or higher, you can place an alternative declaration for the theme in an XML file in res/values-v11, but make the parent theme the holographic theme:
为了让这个主题在运行于Android 3.0(API级别11)或更高时使用较新的全息主题,你可以在res/values-v11内的一个XML文件中放置这种主题的可选声明,但要让父主题是全息主题:
-------------------------------
<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
...
</style>
-------------------------------
Now use this theme like you would any other, and your application will automatically switch to the holographic theme if running on Android 3.0 or higher.
现在像你用其它任意主题那样使用这种主题,并且你的应用程序将自动地切换到全息主题如果它运行在Android 3.0或更高。
A list of the standard attributes that you can use in themes can be found at R.styleable.Theme.
在主题中你可以使用一列能在R.styleable.Theme中找到的标准属性。
For more information about providing alternative resources, such as themes and layouts, based on the platform version or other device configurations, see the Providing Resources document.
想获取关于提供可选资源的更多信息,诸如主题和布局,基于平台版本或其它设备配置,请参见提供资源文档。
-------------------------------
Using Platform Styles and Themes
使用平台样式和主题
The Android platform provides a large collection of styles and themes that you can use in your applications. You can find a reference of all available styles in the R.style class. To use the styles listed here, replace all underscores in the style name with a period. For example, you can apply the Theme_NoTitleBar theme with "@android:style/Theme.NoTitleBar".
Android平台提供一个大的样式和主题集合,你可以在你的应用程序中使用它们。你可以在R.style类中找到所有可用样式的参考文档。为了使用这里列举的样式,请把样式名中所有下环线替换为句号。例如,你可以用"@android:style/Theme.NoTitleBar"应用Theme_NoTitleBar主题。
The R.style reference, however, is not well documented and does not thoroughly describe the styles, so viewing the actual source code for these styles and themes will give you a better understanding of what style properties each one provides. For a better reference to the Android styles and themes, see the following source code:
然而,R.style的参考文档并不被良好地文档化而且没有彻底描述样式,所以查看这些样式和主题的实际源代码将让你更好地理解每个样式提供什么样式属性。想获取对Android样式和主题的较好参考,请参见以下源代码:
* Android Styles (styles.xml)
* Android样式(style.xml)
* Android Themes (themes.xml)
* Android主题(themes.xml)
These files will help you learn through example. For instance, in the Android themes source code, you'll find a declaration for <style name="Theme.Dialog">. In this definition, you'll see all of the properties that are used to style dialogs that are used by the Android framework.
这些文件将帮助你用示例来学习。例如,在Android主题源代码中,你将找到<style name="Theme.Dialog">的声明。在这个声明中,你将看到用于样式化被Android框架所使用的对话框的所有属性。
For more information about the syntax used to create styles in XML, see Available Resource Types: Style and Themes.
想获取关于XML中创建样式所使用的语法的更多信息,请参见可用资源类型:样式和主题。
For a reference of available style attributes that you can use to define a style or theme (e.g., "windowBackground" or "textAppearance"), see R.attr or the respective View class for which you are creating a style.
想获取你可以用于定义样式或主题的可用style属性的参考文档(例如,"windowBackground"或"textAppearance"),请参考R.attr或你创建样式所对应的View类。
Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.
除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。
Android 4.0 r1 - 09 Dec 2011 11:54
-------------------------------
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)