---
title: "Android界面开发之样式定义（Defining Styles）"
author: "hell0Android"
date: "2013-07-09T14:23:57Z"
tags: []
categories: ["移动开发"]
slug: "android-defining-styles"
draft: false
lastmod: "2013-07-09T14:23:57Z"
id: 1901904
url: "https://hell0android.iteye.com/blog/1901904"
---

原文自：[http://android.eoe.cn/topic/ui](http://android.eoe.cn/topic/ui)

 

category:Android Dev Guide  
    style是用于指定[View](http://developer.android.com/reference/android/view/View.html)或window的外观和格式的一系列属性的集合。style可以指定高（height）、填补（padding）、字体颜色、字体大小、背景颜色等等属性。style定义在不同于用来设置布局的XML资源中。  
    Android中的Syles与网页设计中的层叠样式表有着相似的原理——允许你将设计从内容中分离出来。  
    例如，使用一个style，你可以将下面这个布局：

变成这样：

    所有与style相关的属性从XML布局中移出，放到一个名为CodeFont 的style定义中，通过style属性应用。你将在以下章节中看到此类style的定义。  
    theme是一个应用于整个[Activity](http://developer.android.com/reference/android/app/Activity.html)或应用中，而不是某一个单独的[View](http://developer.android.com/reference/android/view/View.html)（正如上面的例子）。当一个style被作为theme来应用时，Activity或应用中的每个View都将应用支持的每个style属性。例如，你能把CodeFont style作为theme应用于一个Activity，那么这个Activity中所有文本都将是绿色等宽字体。

# 定义样式 - Defining Styles

    创建一套style,需保存一个XML文件到你的工程的res/values/ 文件夹中。  
    这个XML文件的根节点必须是 。  
    为每个要创建的style，添加一个用来唯一标识此style的 元素到文件中（这个属性是必需的）。然后为style的每个属性添加一个name 和一个使用的值（这个属性是必需的）。这个 的值可以是一个关键字符串、十六进制颜色、到另一个资源类型的引用或其他值，取决于style的属性。这里有一个单独style的例子：

&lt;?xml version"utf-8"?&gt;

fill\_parent  
wrap\_content  
[#00F](http://wiki.eoe.cn/page/Styles_and_Themes.html#reply00)F00  
monospace

    每个 元素的@style/CodeFont 在一个XML布局中引用（正如上面的介绍）。  
    在parent 属性是可选的，用来指定另一个style资源的ID，前者继承后者的所有属性。你可以复写继承的style属性，如果你想要那样做。  
    记住，你想要用作一个Activity或应用theme的style，与在XML中定义一个View的style方法是一样的。一个如同上面那样定义的style可以应用于一个View的style，或是整个Activity或应用的theme。稍后讨论如何将一个style应用于一个View或一个应用theme中。

## 继承 - Inheritance

    parent 属性让你能够从指定的style中继承属性。你可以通过这种途径从一个现有的style中继承属性，然后定义你想改变或添加的属性。你可以从你自己创建的style或平台内创建的style中继承。（参阅[Using Platform Styles and Themes](http://wiki.eoeandroid.com/Styles_and_Themes#.E4.BD.BF.E7.94.A8.E5.B9.B3.E5.8F.B0.E7.9A.84.E6.A0.B7.E5.BC.8F.E5.92.8C.E4.B8.BB.E9.A2.98_-_Using_Platform_Styles_and_Themes),以获取关于继承Android平台预定义的style的信息。）例如，你可以继承Android平台默认文本外观并修改：

[#00F](http://wiki.eoe.cn/page/Styles_and_Themes.html#reply00)F00

    如果你想要继承你自己定义的style，你不必使用CodeFont 的style，把颜色改为红色，你可以像这样编写新的style：

#FF0000

    注意在parent 属性，因为CodeFont 起始（你已经创建的style），这个style继承所有style属性。这个style复写@style/CodeFont.Red 引用这个新style。  
    你可以像这样继续继承很多次，只要修改句点之前的名称。例如，你可以扩展CodeFont.Red 使字体变大：

30sp

    从CodeFont.Red style中同时继承，然后添加android:textSize 属性。

\* 注意:* 这种技巧仅适用于将你自己定义的资源链接起来。你不能用这种方式继承Android内建的style。要引用一个诸如[TextAppearance](http://developer.android.com/reference/android/R.style.html#TextAppearance)的内建style，你必须使用parent 属性。

## 样式属性 - Style Properties

    到目前，你已明白了一个style是如何定义的，你需要学习由 元素定义的哪些属性是可用的。你很可能已经熟悉了某些，比如[layout\_width](http://developer.android.com/reference/android/R.attr.html#layout_width)和[textColor](http://developer.android.com/reference/android/R.attr.html#textColor)。当然，有更多的style属性供你使用。  
    找到适用于某个特定[View](http://developer.android.com/reference/android/view/View.html)的属性的最佳方法是相应的类的参考，其中列出了所有支持的XML属性。例如，在表格[TextView XML attributes](http://developer.android.com/reference/android/widget/TextView.html#lattrs)中列出的所有属性可以用在[TextView](http://developer.android.com/reference/android/widget/TextView.html)元素（或它的一个子类）的style定义中。其中列出的一个属性是[android:inputType](http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType)，那么你通常可能将[android:inputType](http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType)属性放置在 元素中，像这样：

    你也可以为包含这个属性的[EditText](http://developer.android.com/reference/android/widget/EditText.html)元素创建一个style：

number  
...

    所以你的布局XML现在可以这样实现这个style：

    这个简单的例子看起来增加了工作量，但当你添加越来越多的style属性并考虑到此style在不同地方的可重用性时，你会发现获益是巨大的。  
    关于所有可用的style属性，请参见[R.attr](http://developer.android.com/reference/android/R.attr.html)。记住所有的View对象并不接受相同的style属性，所以你通常应该参考特定的[View](http://developer.android.com/reference/android/view/View.html)类，查看其所支持的style属性。但是，如果你对一个View应用了style，而其并不支持此style中某些属性，那么此View将应用那些它支持的属性，并简单忽略那些不支持的。  
    然而一些style属性只能被当作一个theme来应用，而不支持任何View元素。这些style属性应用到整个窗口，而不是任何类型的View。例如那些用于隐藏应用标题、隐藏状态栏或改变窗口背景的style属性。这些style属性不属于任何View对象。探究这些仅应用作theme的style属性，参见[R.attr](http://developer.android.com/reference/android/R.attr.html)中那些以windowNoTitle 和windowBackground 是仅当style作为theme应用于一个Activity或应用时才有效的style属性。参阅下一节，获得关于style应用作theme的信息。

\* 注意:* 不要忘记对每个android: 命名空间前缀。例如： 。

# 样式和主题应用到UI - Applying Styles and Themes to the UI

    有两种方式来设置style：  
\* 对一个独立的View，添加style 属性到你的布局XML中的View元素中。  
\* 或者，对一个Activity或应用添加 或 元素中。  
    当你应用一个style到布局中一个单独的[View](http://developer.android.com/reference/android/view/View.html)上，由此style定义的属性会仅应用于那个[View](http://developer.android.com/reference/android/view/View.html)。如果一个style应用到一个[ViewGroup](http://developer.android.com/reference/android/view/ViewGroup.html)上，那么子[View](http://developer.android.com/reference/android/view/View.html)元素并不会继承应用此style属性——只有你直接应用了style的元素才会应用其属性。然而，你可以通过将其作为theme来应用的方式应用一个style到所有[View](http://developer.android.com/reference/android/view/View.html)元素上。  
    将一个style作为一个theme来应用，你必须在Android manifest中将其应用到一个[Activity](http://developer.android.com/reference/android/app/Activity.html)或应用中。当你这样做，此Activity或应用中的每个[View](http://developer.android.com/reference/android/view/View.html)都将应用其所支持的属性。例如，如果你应用前面示例中的CodeFont style到一个Activity，那么支持此文本style属性的所有View元素都将应用它们。所有View所不支持的属性都会被忽略。如果一个View仅支持某些属性，那么它就只应用那些属性。

## 为视图套用样式 - Apply a style to a View

    下面是如何在XML布局中为View设置style的方法：

    现在这个TextView将应用名为CodeFont 的style所定义的属性。（参阅前面在[Defining Styles](http://wiki.eoeandroid.com/Styles_and_Themes#.E5.AE.9A.E4.B9.89.E6.A0.B7.E5.BC.8F_-_Defining_Styles)中的示例）。

\* 注意:* style属性不能使用android: 命名空间前缀。

## 在Activity或应用程序中应用主题 - Apply a theme to an Activity or application

    对你的应用程序中所有activity设置一个theme，打开 标签，使之包含android:theme 属性和style名称。例如：

    如果你希望theme仅应用到你的应用程序中的某个Activity中，那么就将 标签里。  
    正如Android提供的其他内建资源一样，有许多你可以使用的预定义theme，而不用自己编写它们。例如，你可以使用Dialog theme使你的Activity看起来像一个对话框：

    或者你想让背景变成透明的，那就使用透明theme：

    如果你喜欢一个theme，但又想调整它，那么你可以将其作为你的自定义theme的parent 。例如，你可以像这样修改传统的light theme来使用你自己定义的颜色：

#b0b0ff

@color/custom\_theme\_color  
@color/custom\_theme\_color

（注意，这里颜色需要作为单独的资源提供，因为android:colorBackground ，它不能得到一种文本颜色。）  
    然后在Android Manifest中使用Theme.Light ：

## 选择基于平台版本的主题 - Select a theme based on platform version

    新版本的Android应用程序提供额外的theme，你可能想使用它们在这些平台上运行，同时与旧版本兼容。你可以通过使用自定义theme资源选择不同的parent theme，根据平台版本之间切换完成。  
    例如，这里声明一个自定义theme，相当于是标准平台上默认的light theme。它将在XML文件的res/values/styles.xml ）：

...

    当程序运行在Android3.0（API等级11）或更高的版本时使用新的holographic theme，你可以在res/values-v11 的XML文件中放置另一个声明theme，但holographic theme的parent theme像这样设置：

...

    现在可以如其他的theme那样使用这个theme，如果你的应用程序运行在Android3.0或更高的版本时，将自动切换到holographic theme。  
    你可以在R.styleable.Theme中找到你能够使用的theme的标准属性列表。  
    获得更多关于如theme和layout提供替代资源，基础平台版本或其他设备配置的详细信息，请参阅Providing Resources文档。

# 使用平台的样式和主题 - Using Platform Styles and Themes

 

    Android平台提供了大量的style和theme供你在应用程序中使用。你可以在[R.style](http://developer.android.com/reference/android/R.style.html/)类中找到所有可用的style。要使用这些style，用句点替换style名称中的下划线。例如，你可以通过"@android:style/Theme.NoTitleBar"应用[Theme\_NoTitleBar](http://developer.android.com/reference/android/R.style.html#Theme_NoTitleBar/) theme。  
    然而，[R.style](http://developer.android.com/reference/android/R.style.html/)没有好的文档，没有详细叙述这些style，所以查看这些style和theme的实际源代码将使你更好理解每个style属性提供了什么功能。为更好参考Android的style和theme，请参阅下列源代码：  
* [Android Styles (styles.xml)](http://android.git.kernel.org/?pblob%3Bfd7b654e49809cb97a35682754b1394af5c8bc88b%3Bhb=HEAD%2F)  
* [Android Themes (themes.xml)](http://android.git.kernel.org/?pblob%3Bf6b3d7407d1c895a3c297e60d5beac98e2d34c271%3Bhb=HEAD%2F)  
    这些文件将通过例子帮助你学习。举个例子，在Android theme源代码中，你将会找到一个声明。在这个定义中，你将看到所有由Android框架使用的用于对话框的style属性。  
    为获得更多关于在XML中创建style的语法，参阅[Available Resource Types:Style and Themes](http://developer.android.com/guide/topics/resources/available-resources.html#stylesandthemes/)。  
    关于你可以用来定义style或theme的可用style属性（例如，"windowBackground" 或 "textAppearance"），参阅[R.attr](http://developer.android.com/reference/android/R.attr.html/)或者对应于你正在为其创建一个style的View类。

←返回User Interface | 用户界面 - User Interface