Styles and Themes//风格和主题

Styles and Themes//风格和主题

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.
//一个style是指定视图或者窗口的属性的集合。一个style可以指定诸如高度,填充,字体颜色,字体尺寸,背景色等等属性。一个style在xml资源里面被定义,这个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.
//在安卓里面的style和web设计里面的级联样式表分享了一个相似的哲学----允许你将设计和内容分开。
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的属性被应用。你可以在下面的部分看到为这个样式的定义。
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.
//一个theme是一个在整个Activity或者应用里面应用的样式,而不仅仅是一个个别的View(像下面的例子中那样).当一个样式作为一个主题使用时,
在Activity或者应用里面的每一个View将会适用它支持的每个样式属性.例如,你可以将CodeFont样式最为一个Activity的主题使用,那么在这个Activity里面的所有text都将拥有绿色的等宽字体。

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里面明确定义的。一个像上面那样定义的样式是可以在一个单独的视图里面应用,也可以在整个Activity或者应用里面使用。如何在一个单独的视图或者作为一个应用的主题使用一个样式将在稍后讨论。

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属性允许你指定一个样式,你的样式可以继承这个样式的属性。你可以通过这种方法从一个已经存在的样式里面继承属性并且定义那些你想增加或者改变的属性。你可以从你自己建立的样式或者是平台建立的样式里面继承。(见 Using Platform Styles and Themes,下面,关于如何从安卓平台定义的样式里面继承的信息)。例如,你可以继承安卓平台默认的文本外观然后修改它:
<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(你自己已经建立的样式)样式的名字开头,所以这个样式从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 到bigger,用:
  <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 asTextAppearance, you must use the parent attribute.
//注意:这种通过链接名字的继承方法只在你自己定义的资源样式里面起作用。你不能通过这种方法继承安卓内置的样式。引用内置的样式,例如TextAppearance,你必须使用父属性。

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.
//现在你知道了如何建立属性,你需要学习什么样的样式----被<item>元素定义的---是合适的.你大概对一些熟悉了,例如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:
//发现实用于指定视图的属性的最好的地方是相应的类引用,类引用列举了支持的所有的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里面你可以实现这个样式:
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.
//






1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值