[翻译][Pro Android 4]——Chapter 3 理解Android资源

理解资源

    资源在Android架构中发挥着很重要的作用。在Android中资源一般是一个绑定在可执行程序上的文件(比如一个音乐文件或记录窗口布局的文件)或值(比如一个对话框的标题)。这些绑定在可执行程序中的文件或值可以让编程人员随意修改或替换,且不用重新编译应用程序。

    我们熟悉的资源包括字符串,颜色,位图以及布局等。作为代替应用程序中的硬编码字符串,资源文件允许使用资源ID作为代替方式。这可以间接地修改程序中的资源,且不用修改源代码。

    Android中有非常非常多的资源,而我们最开始要讨论的是最常见的一种资源:字符串。

字符串资源

    Android允许在一个或多个XML文件中定义字符串资源。这些包含字符串资源的定义的XML文件存放于子目录/res/values下。该XML文件名称可以任意命名,尽管我们会经常看到这个文件叫string.xml。列表3-1展示了一个字符串资源文件的例子。

列表3–1:strings.xml示例

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

<resources> 

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

    <string name="app_name">hello appname</string> 

</resources> 

注意: 在某些Eclipse的发行版本中,  <resources> 节点需要xmln规范才能使用。但指向似乎并不重要了。 下面这两种形式都可以工作:
<resources xmlns="http://schemas.android.com/apk/res/android" > 和
<resources xmlns="default namespace" >

    虽然在文件的第一行就指出这是一个可自由选择编码格式的XML文件,但是如果没有这一行,Android依然可以正常运行。

    当这个文件被创建或被修改后,Eclipse的ADT插件会在自动在应用程序的一个叫R.java的包内为指定的两个字符串创建唯一的ID。注意下面例子中的R.java文件的位置,这是一个高级别的工程目录结构,我们叫它MyProject。

\MyProject
   \src
         \com\mycompany\android\my-root-package
         \com\mycompany\android\my-root-package\another-package
   \gen
        \com\mycompany\android\my-root-package\R.java
   \assets
   \res
   \AndroidManifest.xml
...etc

注意:  无论有多少个资源文件,都只能有一个R.java文件。 

For the string-resource file in Listing 3–1, the updated R.java file has the entries in
Listing 3–2.

package com.mycompany.android.my-root-package; 

public final class R { 

   //...other entries depending on your project and application 

    

    public static final class string  

   { 

      //...other entries depending on your project and application 

       

        public static final int hello=0x7f040000; 

        public static final int app_name=0x7f040001; 

       

      //...other entries depending on your project and application 

    } 

   //...other entries depending on your project and application 

} 

Notice, first, how R.java defines a top-level class in the root package: public static
final class R. Within that outer class of R, Android defines an inner class, static final
class string. R.java creates this inner static class as a namespace to hold string
resource IDs. 
The two static final ints defined with variable names hello and app_name are the
resource IDs that represent the corresponding string resources. You can use these
resource IDs anywhere in the source code through the following code structure: 
R.string.hello
The generated IDs point to ints rather than strings. Most methods that take strings
also take these resource identifiers as inputs. Android resolves those ints to strings
where necessary. 
It is merely a convention that most sample applications define all strings in one
strings.xml file. Android takes any number of arbitrary files as long as the structure of
the XML file looks like Listing 3–1 and the files reside in the /res/values subdirectory.
The structure of this file is easy to follow. You have the root node <resources> followed
by one or more <string> child elements. Each <string> element or node has a property
called name that ends up as the id attribute in R.java.
To see that multiple string resource files are allowed in this subdirectory, you can place
another file with the following content in the same subdirectory and call it strings1.xml
(see Listing 3–3).

Listing 3–3. Example of an Additional  strings.xml  File

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

<resources> 

    <string name="hello1">hello 1</string> 

    <string name="app_name1">hello appname 1</string> 

</resources> 

The Eclipse ADT plug-in validates the uniqueness of these IDs at compile time and
places them in R.java as two additional constants: R.string.hello1 and
R.string.app_name1. 

你可能感兴趣的:(android)