`

【翻译】(29)访问资源

 
阅读更多

【翻译】(29)访问资源

 

see

http://developer.android.com/guide/topics/resources/accessing-resources.html

 

原文见

http://developer.android.com/guide/topics/resources/accessing-resources.html

 

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

 

Accessing Resources

 

访问资源

 

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

 

Quickview

 

快速概览

 

* Resources can be referenced from code using integers from R.java, such as R.drawable.myimage

 

* 资源可以从代码中被引用,使用来自R.java的整型,诸如R.drawable.myimage

 

* Resources can be referenced from resources using a special XML syntax, such as @drawable/myimage

 

* 资源可以从资源中被引用,使用特殊XML语法,诸如@drawable/myimage

 

* You can also access your app resources with methods in Resources

 

* 你还可以使用Resources的方法访问你的应用资源

 

Key classes

 

关键类

 

Resources

 

In this document

 

本文目录

 

* Accessing Resources from Code 从代码中访问资源

* Accessing Resources from XML 从XML中访问资源

* Referencing style attributes 引用样式属性

* Accessing Platform Resources 访问平台资源

 

See also 

 

另见

 

Providing Resources 提供资源

Resource Types 资源类型

 

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

 

Once you provide a resource in your application (discussed in Providing Resources), you can apply it by referencing its resource ID. All resource IDs are defined in your project's R class, which the aapt tool automatically generates.

 

一旦你在你的应用程序中提供一个资源(在提供资源中讨论),你可以通过引用它的引用ID来应用它。所有引用ID被定义在你的工程的R类中,它是aapt工具自动生成的。

 

When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory. For each type of resource, there is an R subclass (for example, R.drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R.drawable.icon). This integer is the resource ID that you can use to retrieve your resource.

 

当你的应用程序被编译时,aapt生成R类,它包含用于你的res/目录中的所有资源的资源ID。对于每个类型的资源,有一个R子类(例如,R.drawable用于所有可绘画对象资源)并且对于那种类型的资源,有一个静态整型(例如,R.drawable.icon)。这个整型是你可以用来取出你的资源的资源ID。

 

Although the R class is where resource IDs are specified, you should never need to look there to discover a resource ID. A resource ID is always composed of:

 

虽然R类是资源ID被指定的地方,但是你应该从不需要看那里来发现资源ID。资源ID总是包含:(注:这里的ID是指变量名)

 

* The resource type: Each resource is grouped into a "type," such as string, drawable, and layout. For more about the different types, see Resource Types.

 

* 资源类型:每个资源被分组为一个“类型”,诸如字符串,可绘画对象,和布局,想获取关于不同类型的更多信息,请参见资源类型。

 

* The resource name, which is either: the filename, excluding the extension; or the value in the XML android:name attribute, if the resource is a simple value (such as a string).

 

* 资源名称,它是其中之一:文件名,不包括扩展名;或者XML中的android:name属性的值,如果资源是一个简单值(诸如一个字符串)。

 

There are two ways you can access a resource:

 

你可以用两种方式访问一个资源:

 

* In code: Using an static integer from a sub-class of your R class, such as:

 

* 在代码中:使用来自你的R类子类的一个静态整型,诸如:

 

R.string.hello

 

string is the resource type and hello is the resource name. There are many Android APIs that can access your resources when you provide a resource ID in this format. See Accessing Resources in Code.

 

string是资源类型,而hello是资源名称。当你以这种格式提供一个资源ID时,有许多Android API可以访问你的资源。参见在代码中访问资源。

 

* In XML: Using a special XML syntax that also corresponds to the resource ID defined in your R class, such as:

 

* 在XML中:使用一个特殊XML语法,它可以对应定义在你的R类中的资源ID,诸如:

 

@string/hello

 

string is the resource type and hello is the resource name. You can use this syntax in an XML resource any place where a value is expected that you provide in a resource. See Accessing Resources from XML.

 

string是资源类型,而hello是资源名称。你可以在一个XML资源中的任意地方使用这个语法,那里期待你在资源中提供一个值。见从XML中访问资源。

 

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

 

Accessing Resources in Code

 

在代码中访问资源

 

You can use a resource in code by passing the resource ID as a method parameter. For example, you can set an ImageView to use the res/drawable/myimage.png resource using setImageResource():

 

你可以通过传递资源ID作为方法的参数,在代码中使用一个资源。例如,你可以使用setImageResource(),设置一个ImageView使用res/drawable/myimage.png资源。

 

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

 

ImageView imageView = (ImageView) findViewById(R.id.myimageview);

imageView.setImageResource(R.drawable.myimage);

 

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

 

You can also retrieve individual resources using methods in Resources, which you can get an instance of with getResources().

 

你还可以使用Resources中的方法取出单独的资源,你可以用getResources()取得它的一个实例。

 

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

 

Access to Original Files

 

访问原始文件

 

While uncommon, you might need access your original files and directories. If you do, then saving your files in res/ won't work for you, because the only way to read a resource from res/ is with the resource ID. Instead, you can save your resources in the assets/ directory.

 

在罕见的情况下,你可能需要访问你的原始文件和目录。如果你要这样做,那么把你的文件保存在res/中对你来说没有用,因为从res/中读取资源的唯一方式是使用资源ID。取而代之,你可以保存你的资源在assets/目录中。

 

Files saved in the assets/ directory are not given a resource ID, so you can't reference them through the R class or from XML resources. Instead, you can query files in the assets/ directory like a normal file system and read raw data using AssetManager.

 

保存在assets/目录中的文件不会被指定资源ID,所以你不可以通过R类或从XML资源中引用它们。取而代之,你可以就像普通文件系统那样查询assets/目录中的文件并且使用AssetManager读取原始数据。(注:不知道为什么,Android文档中并没有专门提及file:///android_assets/的使用)

 

However, if all you require is the ability to read raw data (such as a video or audio file), then save the file in the res/raw/ directory and read a stream of bytes using openRawResource().

 

然而,如果所有你所需的是读取原始数据的功能(诸如一个视频或音频文件),那么保存文件在res/raw/目录中并且使用openRawResource()读取字节流。

 

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

 

Syntax

 

语法

 

Here's the syntax to reference a resource in code:

 

这里是在代码中引用资源的语法:

 

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

 

[<package_name>.]R.<resource_type>.<resource_name>

 

[<包名>.]R.<资源类型>.<资源名称>

 

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

 

* <package_name> is the name of the package in which the resource is located (not required when referencing resources from your own package).

 

* <包名>是资源所在包的名称(当引用资源来自你自己的包时不需要)

 

* <resource_type> is the R subclass for the resource type.

 

* <资源类型>是用于资源类型的R子类。

 

* <resource_name> is either the resource filename without the extension or the android:name attribute value in the XML element (for simple values).

 

* <资源名称>是不带扩展名的资源文件名或在XML元素中android:name属性的值(用于简单值)。

 

See Resource Types for more information about each resource type and how to reference them.

 

参见资源类型以获取关于每个资源类型和如何引用它们的更多信息。

 

Use cases

 

用例

 

There are many methods that accept a resource ID parameter and you can retrieve resources using methods in Resources. You can get an instance of Resources with Context.getResources().

 

有许多接受资源ID参数的方法,而且你可以使用Resources中的方法取出资源。你可以用Context.getResources()获得Resources的一个实例。(注,Activity对象可以直接调用getText(),等效于getResources().getText())

 

Here are some examples of accessing resources in code:

 

有一些在代码中访问资源的例子:

 

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

 

// Load a background for the current screen from a drawable resource

// 从一个可绘画资源中为当前屏幕加载一个背景图

getWindow().setBackgroundDrawableResource(R.drawable.my_background_image) ;

 

// Set the Activity title by getting a string from the Resources object, because

//  this method requires a CharSequence rather than a resource ID

// 通过从Resources中得到一个字符串来设置Activity标题,

//  因为这个方法需要一个CharSequence而非资源ID

getWindow().setTitle(getResources().getText(R.string.main_title));

 

// Load a custom layout for the current screen

// 为当前屏幕加载一个自定义布局

setContentView(R.layout.main_screen);

 

// Set a slide in animation by getting an Animation from the Resources object

// 通过从Resources对象中得到一个Animation对象,把幻灯片设置为动画

mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,

        R.anim.hyperspace_in));

 

// Set the text on a TextView object using a resource ID

// 是哟个一个资源ID设置TextView对象上的文本

TextView msgTextView = (TextView) findViewById(R.id.msg);

msgTextView.setText(R.string.hello_message);

 

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

 

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

 

Caution: You should never modify the R.java file by hand—it is generated by the aapt tool when your project is compiled. Any changes are overridden next time you compile.

 

警告:你应该从不手动修改R.java——当你的工程被编译时它由aapt工具生成。任何改变在你下一次编译时被覆盖。

 

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

 

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

 

Accessing Resources from XML

 

从XML中访问资源

 

You can define values for some XML attributes and elements using a reference to an existing resource. You will often do this when creating layout files, to supply strings and images for your widgets.

 

你可以使用指向一个现存资源的引用,为一些XML属性和元素定义值。当创建布局文件时你将经常做这种事情,以为你的部件提供字符串和图片。

 

For example, if you add a Button to your layout, you should use a string resource for the button text:

 

例如,如果你添加一个Button到你的布局,那么你应该为按钮文本使用一个字符串资源:

 

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

 

<Button

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/submit" />

Syntax

 

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

 

Here is the syntax to reference a resource in an XML resource:

 

这里是引用XML资源内的资源的语法:

 

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

 

@[<package_name>:]<resource_type>/<resource_name>

 

@[<包名>:]<资源类型>/<资源名称>

 

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

 

* <package_name> is the name of the package in which the resource is located (not required when referencing resources from the same package)

 

* <包名>是资源所在包的名称(不需要,如果引用的资源来自同一个包)

 

* <resource_type> is the R subclass for the resource type

 

* <资源类型>是用于资源类型的R子类

 

* <resource_name> is either the resource filename without the extension or the android:name attribute value in the XML element (for simple values).

 

* <资源名称>是不带扩展名的资源文件名或XML元素中android:name属性的值(用于简单值)。

 

See Resource Types for more information about each resource type and how to reference them.

 

参见资源类型以获取关于每个资源类型和如何引用它们的更多信息。

 

Use cases

 

用例

 

In some cases you must use a resource for a value in XML (for example, to apply a drawable image to a widget), but you can also use a resource in XML any place that accepts a simple value. For example, if you have the following resource file that includes a color resource and a string resource:

 

在一些情况下你必须对XML中的值使用资源(例如,为了应用一个可绘画图片到一个部件),但你还可以把XML中的资源用在任意接受简单值的位置。例如,如果你有以下资源文件包含一个颜色资源和一个字符串资源:

 

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

 

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

<resources>

   <color name="opaque_red">#f00</color>

   <string name="hello">Hello!</string>

</resources>

 

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

 

You can use these resources in the following layout file to set the text color and text string:

 

你可以在以下布局文件中使用这些资源以设置文本颜色和文本字符串:

 

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

 

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

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

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:textColor="@color/opaque_red"

    android:text="@string/hello" />

 

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

 

In this case you don't need to specify the package name in the resource reference because the resources are from your own package. To reference a system resource, you would need to include the package name. For example:

 

在这种情况下你不需要在资源引用中指定包名,因为资源来自你自己的包。为了引用系统资源,你需要包含包名。例如:

 

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

 

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

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

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:textColor="@android:color/secondary_text_dark"

    android:text="@string/hello" />

 

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

 

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

 

Note: You should use string resources at all times, so that your application can be localized for other languages. For information about creating alternative resources (such as localized strings), see Providing Alternative Resources.

 

注意:你应该总是使用字符串资源,以使你的应用程序可以本地化为其它语言。想获取关于创建可选资源的信息(诸如本地化的字符串),请参见提供可选资源。

 

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

 

You can even use resources in XML to create aliases. For example, you can create a drawable resource that is an alias for another drawable resource:

 

你甚至可以使用XML中的资源来创建别名。例如,你可以创建一个可绘画资源,它是一个指向另一个可绘画资源的别名:

 

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

 

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

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

    android:src="@drawable/other_drawable" />

 

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

 

This sounds redundant, but can be very useful when using alternative resource. Read more about Creating alias resources.

 

这听起来有点冗余,但是在使用可选资源时可能非常有用。请阅读关于创建别名资源的更多信息。

 

Referencing style attributes

 

引用样式属性

 

A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."

 

一个样式属性资源允许你引用当前应用的主题中的一个属性的值。引用一个样式属性允许你自定义用户界面元素的外观,通过样式化它们以匹配当前主题提供的标准变化,而非提供硬编码的值。引用一个样式属性本质上是在说,“使用在当前主题中被这个属性定义的样式”。

 

To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional. For instance:

 

为了引用一个样式属性,名称语法几乎和普通资源格式相同,但不是用@号(@),而是用一个问号(?),而且资源的类型部分是可选的。例如:

 

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

 

?[<package_name>:][<resource_type>/]<resource_name>

 

?[<包名>:][<资源类型>/]<资源名称>

 

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

 

For example, here's how you can reference an attribute to set the text color to match the "primary" text color of the system theme:

 

例如,这里是你如何可以引用一个属性以设置文本颜色以匹配系统样式的“主”文本颜色:

 

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

 

<EditText id="text"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textColor="?android:textColorSecondary"

    android:text="@string/hello_world" />

 

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

 

Here, the android:textColor attribute specifies the name of a style attribute in the current theme. Android now uses the value applied to the android:textColorSecondary style attribute as the value for android:textColor in this widget. Because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type (which would be ?android:attr/textColorSecondary)—you can exclude the attr type.

 

这里,android:textColor属性指定当前主题中一个样式属性的名称。Android现在使用应用到android:textColorSecondary样式属性的值作为这个部件中android:textColor的值。因为系统资源工具知道在这个上下文中期待一个属性资源,所以你不需要显式地声明类型(它将是?android:attr/textColorSecondary)——你可以不包含attr类型。

 

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

 

Accessing Platform Resources

 

访问平台资源

 

Android contains a number of standard resources, such as styles, themes, and layouts. To access these resource, qualify your resource reference with the android package name. For example, Android provides a layout resource you can use for list items in a ListAdapter:

 

Android包含一些标准资源,诸如样式、主题,以及布局。为了访问这些资源,用android包名修饰你的资源引用。例如,Android提供一个布局资源,你可以用于ListAdapter中的列表条目:

 

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

 

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray));

 

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

 

In this example, simple_list_item_1 is a layout resource defined by the platform for items in a ListView. You can use this instead of creating your own layout for list items. (For more about using ListView, see the List View Tutorial.)

 

在这个示例中,simple_list_item_1是一个由平台定义的布局资源用于ListView中的条目。你可以使用它而非为列表条目创建你自己的布局。(想获取关于ListView的更多信息,请参见列表视图教程。)

 

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 - 04 Jan 2012 0:53

 

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

 

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/


分享到:
评论

相关推荐

    华中大在线资源整合系统V2.0

    软件类别:网络工具》》网络辅助 软件名称:华中大在线资源整合系统V2.0 软件介绍:华中大在线... 以上资源电信网,教育网同学都可以轻松访问与利用。 同时,软件是一款绿色软件无需安装,单EXE文件方便同学间传播。

    js谷歌翻译插件.rar

    按照压缩包内的html示例调用即可, 这里只将部分耗时较长的js和容易请求失败的文件放在了本地,其他的png,gif还是通过网络访问的 翻译后控制台还是会一些报错,请求访问失败,不影响正常使忽略即可

    [示例][PHP]drest-master将Doctrine实体展现为REST资源结点的库.zip

    REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度类审视整个网络,它将分布在网络中某个节点的资源通过URL进行标识,客户端...

    ASP网络硬盘文件资源管理系统(源代码+thesis++replyPPT+外文翻译).zip

    这包括使用高效的算法和数据结构、使用缓存技术和数据库索引、进行代码优化和资源管理等。 通过采用这些技术方案,我们的项目将能够提供一个高性能、可扩展和可靠的Web应用程序。我们将遵循最佳的软件开发实践,进行...

    雪人计算机辅助翻译(CAT) 中文-韩语版 v1.30.zip

    它能够辅助译员优质、高效、轻松地完成翻译工作,帮助企业及个人充分利用资源、降低成本、成倍提高工作效率。适用于需要精确翻译的机构和个人。  雪人CAT软件特色:1. 简单易用、速度快:瞬间完成在百万级的记忆库...

    计算机专业毕设精选-ASP网络硬盘文件资源管理系统(源代码+论文+开题报告+答辩PPT+外文翻译).rar

    3. **共享功能**:用户可以将文件设置为共享状态,供其他用户访问和下载,实现资料的快速分享和传播。 4. **搜索功能**:系统内置强大的搜索引擎,支持关键词搜索和多条件筛选,帮助用户快速找到所需文件。 5. **...

    rfc中文文档目录,包含部分翻译

    RFC29 响应 RFC 28 RFC30 文档规范 RFC32 关于SRI所提议的实时时钟的一些想法 RFC34 关于ARC时钟的一些初步记录摘要 RFC35 网络会议 RFC36 协议注解 RFC37 网络会议结尾等 RFC38 NWG/RFC #36 网络协议的注解 ...

    Java资源包01

    google-api-translate-java(Java 语言对Google翻译引擎的封装类库) 语音识别程序 SpeechLion.tar SpeechLion 是一个语音识别程序,主要用来处理桌面命令,基于 Sphinx-4 语音识别引擎开发。用户可以通过该软件来...

    STM32CubeMX用户手册中文版UM1718-翻译版.rar

    纯人工翻译中文版本,STM32CubeMX用户手册中文版,STM32CubeMX用户手册中文版UM1718-翻译版.菜鸟到高手,显得有些霸气哈,不过的确如此,官方带给我们一个比较实用的stm32的工具。这个工具就是STM32CubeMX,昨天玩...

    系统架构与5G异构云无线入网关键技术(物联网电子类外文翻译及原文).zip

    资源包含文件:英文原文word和翻译word两个文档 与第四代蜂窝系统相比,第五代无线通信系统的频谱和能源效率增长至少为10倍,区域吞吐量增长至少为25倍。为了实现这些目标,本文提出了一种H-CRAN作为高级无线接入网...

    drest-master将Doctrine实体展现为REST资源结点的库.zip

    它可以通过PHP对象轻松访问所有的数据库,例如MYSQL,REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移”REST从资源的角度类审视整个网络,...

    layeredFS:layeredFS 游戏翻译数据包生成工具

    当游戏请求访问资源文件时,将检测SD卡上的翻译数据目录下是否有同名的翻译文件,如果有则进行重定向(如果没有的话仍然访问原始的文件)。只要提供包含修改过的资源文件的翻译数据,即可实现汉化

    关于计算机的外文翻译

    基于.NET的可重用数据访问层的构建 解决,没有外文资源的困惑

    访问地址簿/联系人列表(标准版)

    该文章翻译自iOS in Practice 一书,资源来自Cocoachina。通过教你制作一个上架应用Dial4来学习访问地址簿/联系人列表(标准版)。

    java及web中英对照译文

    JSP(JavaServer Pages)是一种基于...网页还能通过tags和scriptlets访问存在于服务端的资源的应用逻辑。JSP将网页逻辑与网页设计和显示分离,支持可重用的基于组件的设计,使基于Web的应用程序的开发变得迅速和容易。

    github-translations:翻译GitHub的文章

    该项目旨在将GitHub发布的资源,指南和帮助文章翻译成不同的语言,以帮助更多的人访问它。 目前,我遵循的唯一想法或指南是将每篇文章放在其语言的分支中,并且该路径的路径基于该文章的原始路径。 因此, : 在...

    毕设源码-VBIC卡管理系统(源代码+系统+中英文翻译+答辩PPT).rar

    本资源包包含了VBIC卡管理系统的源代码、系统安装包、中英文翻译文档以及答辩PPT,为用户的二次开发和定制提供了极大的便利。我们相信,通过使用本系统,您将能够实现卡片管理的智能化、安全化和高效化,提升整体...

    ResXResourceManager:在一个中央位置管理所有基于ResX的资源的本地化

    此工具提供对解决方案中所有基于ResX的字符串资源的集中访问。 您可以快速浏览所有资源文件,并在结构合理的数据网格中查看内容。 所有可用的语言以列的形式并排显示,以便轻松查找未翻译的字符串或清除孤立的条目...

    访问地址簿/联系人列表(吐槽版)

    该文章翻译自iOS in Practice 一书,资源来自Cocoachina。通过教你制作一个上架应用Dial4来学习访问地址簿/联系人列表(吐槽版)。

    非常好的Ruby中文版项目资源,分享出来.zip

    本指南基于Mark Slagell的英文译本翻译而来,指南最初的版本由Ruby的发明者Yukihiro Matsumoto用日语编写完成。 为了减少翻译过程中的错误,我保留了英文版本的内容。因此不要对本文档有任何的抱怨或不满,Fork并...

Global site tag (gtag.js) - Google Analytics