【翻译】(34)颜色状态列表资源

【翻译】(34)颜色状态列表资源

 

see

http://developer.android.com/guide/topics/resources/color-list-resource.html

 

原文见

http://developer.android.com/guide/topics/resources/color-list-resource.html

 

-------------------------------

 

Color State List Resource

 

颜色状态列表资源

 

-------------------------------

 

See also

 

另见

 

Color (simple value)

 

颜色(简单值)

 

-------------------------------

 

A ColorStateList is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View object to which it is applied. For example, a Button widget can exist in one of several different states (pressed, focused, or niether) and, using a color state list, you can provide a different color during each state.

 

一个ColorStateList是一个你可以定义在XML中的对象,你可以应用它作为一个颜色,但实际上将会改变颜色,依赖于它被应用在的View对象的状态。例如,一个Button部件可以存在于几个不同状态之一(按下,获取焦点,或都不是)(注:neither貌似拼错?),并且使用一个颜色状态列表,你可以在各个状态期间提供不同的颜色。

 

You can describe the state list in an XML file. Each color is defined in an <item> element inside a single <selector> element. Each <item> uses various attributes to describe the state in which it should be used.

 

你可以在一个XML文件中描述状态列表。每个颜色定义在一个单一<selector>元素内的<item>元素中。每个<item>使用不同的属性以描述它应该被使用在的状态。

 

During each state change, the state list is traversed top to bottom and the first item that matches the current state will be used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.

 

在每个状态改变期间,状态列表被由上而下遍历,并且第一个匹配当前状态的条目将被使用——选择不基于“最佳匹配”,只是简单地选择第一个满足状态最低标准的条目。

 

-------------------------------

 

Note: If you want to provide a static color resource, use a simple Color value.

 

注意:如果你希望提供一个静态颜色资源,请使用一个简单Color值。

 

-------------------------------

 

* file location:

 

* 文件位置:

 

res/color/filename.xml

 

res/color/<文件名>.xml

 

The filename will be used as the resource ID.

 

文件名将被用作资源ID。

 

* compiled resource datatype:

 

* 被编译的资源数据类型:

 

Resource pointer to a ColorStateList.

 

指向ColorStateList的资源指针。

 

* resource reference:

 

* 资源引用:

 

In Java: R.color.filename

 

在Java中:R.color.<文件名>

 

In XML: @[package:]color/filename

 

在XML中: @[<包名>:]color/<文件名>

 

* syntax:

 

* 语法:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item

        android:color="hex_color"

        android:state_pressed=["true" | "false"]

        android:state_focused=["true" | "false"]

        android:state_selected=["true" | "false"]

        android:state_checkable=["true" | "false"]

        android:state_checked=["true" | "false"]

        android:state_enabled=["true" | "false"]

        android:state_window_focused=["true" | "false"] />

</selector>

 

-------------------------------

 

* elements:

 

* 元素:

 

* <selector>

 

Required. This must be the root element. Contains one or more <item> elements.

 

必需的。它必须是根元素。包含一个或多个<item>元素。

 

* attributes:

 

* 属性:

 

* xmlns:android

 

String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".

 

字符串。必需的。定义XML名字空间,必须是"http://schemas.android.com/apk/res/android"。

 

* <item>

 

Defines a color to use during certain states, as described by its attributes. Must be a child of a <selector> element.

 

定义在某个状态期间使用的颜色,正如它的属性所描述的状态。必须是<selector>元素的子元素。

 

* attributes:

 

* 属性:

 

* android:color

 

Hexadeximal color. Required. The color is specified with an RGB value and optional alpha channel.

 

十六进制颜色。必需的。颜色用一个RGB值(注:RGB是红绿蓝的英文首字母缩写)指定以及可选的透明通道来指定。

 

The value always begins with a pound (#) character and then followed by the Alpha-Red-Green-Blue information in one of the following formats:

 

值总是用一个井号(#)(注:pound原意是英镑,#号在美式英语可读作pound sign,在其它地方称hash,在音乐中读作sharp)字符开头,并且后面是透明-红-绿-蓝信息,用以下格式之一表示。

 

* #RGB

* #ARGB

* #RRGGBB

* #AARRGGBB

 

* android:state_pressed

 

Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.

 

布尔型。"true"如果当对象被按下(诸如当一个按钮被触碰/点击)时这个条目应该被使用;"false"如果这个条目在非按下状态下默认应该被使用。

 

* android:state_focused

 

Boolean. "true" if this item should be used when the object is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.

 

布尔型。"true"如果当对象获取焦点时(诸如当按钮使用轨迹球/方向板高亮时)这个条目应该被使用;"false"如果这个条目应该被默认使用于非获取焦点状态。

 

* android:state_selected

 

Boolean. "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.

 

布尔型。"true"如果当对象被选中时(诸如当打开标签页时)这个条目应该被使用;"false"如果当对象未选中时这个条目应该被使用。

 

* android:state_checkable

 

Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)

 

布尔型。"true"如果当对象可勾选时这个条目应该被使用;"false"如果当对象不可勾选时这个条目应该被使用。(仅当对象可以在可勾选和非可勾选部件之间过渡才有用。)

 

* android:state_checked

 

Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.

 

布尔型。"true"如果当对象被勾选时这个条目应该被使用;"false"如果当对象未被勾选时它应该被使用。

 

* android:state_enabled

 

Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.

 

布尔型。"true"如果当对象使能时这个条目应该被使用(可以接收触碰/点击事件);"false"如果当对象被屏蔽时它应该被使用。

 

* android:state_window_focused

 

Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).

 

布尔型。"true"如果当应用程序窗口拥有焦点时(应用程序在前台)这个条目应该被使用,"false"如果当应用程序不拥有焦点时这个条目应该被应用(例如,如果通知窗帘被拉下或一个对话框显示)。

 

-------------------------------

 

Note: Remember that the first item in the state list that matches the current state of the object will be applied. So if the first item in the list contains none of the state attributes above, then it will be applied every time, which is why your default value should always be last (as demonstrated in the following example).

 

注意:记住在状态列表中第一个匹配对象当前状态的条目将被应用。所以如果列表中第一个条目没有包含上面所述的任何状态属性,那么它将每次都会被应用,这就是为什么你的默认值应该总是放在最后(正如下面的示例中所演示的那样)。

 

-------------------------------

 

* example:

 

* 示例:

 

XML file saved at res/color/button_text.xml:

 

XML文件保存在res/color/button_text.xml:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"

          android:color="#ffff0000"/> <!-- pressed --> <!-- 按下 -->

    <item android:state_focused="true"

          android:color="#ff0000ff"/> <!-- focused --> <!-- 获取焦点 -->

    <item android:color="#ff000000"/> <!-- default --> <!-- 默认 -->

</selector>

 

-------------------------------

 

This layout XML will apply the color list to a View:

 

这个布局XML将应用颜色列表到一个View:

 

-------------------------------

 

<Button

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/button_text"

    android:textColor="@color/button_text" />

 

-------------------------------

 

* see also:

 

* 另见:

 

* Color (simple value) 颜色(简单值)

* ColorStateList 

* State List Drawable 状态列表可绘画对象

 

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 - 13 Jan 2012 0:46

 

-------------------------------

 

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来源许可证描述的条款进行修改)

 

(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:

* ソフトウェア技術ドキュメントを勝手に翻訳

http://www.techdoctranslator.com/android

* Ley's Blog

http://leybreeze.com/blog/

* 农民伯伯

http://www.cnblogs.com/over140/

* Android中文翻译组

http://androidbox.sinaapp.com/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值