【翻译】(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/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值