Android 资源基础知识Android Resource Basics

几乎所有 Android 应用程序都将具有某种类型的资源;至少,它们通常以 XML 文件的形式具有用户界面布局。Almost all Android applications will have some sort of resources in them; at a minimum they often have the user interface layouts in the form of XML files. 首次创建 Xamarin Android 应用程序时,会通过 Xamarin Android 项目模板设置默认资源:When a Xamarin.Android application is first created, default resources are setup by the Xamarin.Android project template:

在资源文件夹中创建组成默认资源的五个文件:The five files that make up the default resources were created in the Resources folder:

  • Icon – 应用程序的默认图标Icon.png – The default icon for the application

  • Main.axml – 应用程序的默认用户界面布局文件。Main.axml – The default user interface layout file for an application. 请注意,Android 使用 .xml文件扩展名时,Xamarin 使用main.axml文件扩展名。Note that while Android uses the .xml file extension, Xamarin.Android uses the .axml file extension.

  • String .xml – 一个字符串表,以帮助本地化应用程序Strings.xml – A string table to help with localization of the application

  • AboutResources – 这是不必要的,可以安全地删除。AboutResources.txt – This is not necessary and may safely be deleted. 它只是提供资源文件夹以及其中的文件的高级概述。It just provides a high level overview of the Resources folder and the files in it.

  • Resource.designer.cs – 此文件通过 Xamarin 自动生成和维护,并保存分配给每个资源的唯一 ID。Resource.designer.cs – This file is automatically generated and maintained by Xamarin.Android and holds the unique ID's assigned to each resource. 这与在 Java 中编写的 Android 应用程序所使用的 R .java 文件非常相似,但用途相同。This is very similar and identical in purpose to the R.java file that an Android application written in Java would have. 它是由 Xamarin Android 工具自动创建的,并将随时重新生成。It is automatically created by the Xamarin.Android tools and will be regenerated from time to time.

创建和访问资源Creating and Accessing Resources

创建资源与将文件添加到相关资源类型的目录非常简单。Creating resources is as simple as adding files to the directory for the resource type in question. 下面的屏幕截图显示了德语区域设置的字符串资源已添加到项目。The screen shot below shows string resources for German locales were added to a project. string .xml添加到文件中时,"生成" 操作已自动设置为 Xamarin 的AndroidResource 。 Android 工具:When Strings.xml was added to the file, the Build Action was automatically set to AndroidResource by the Xamarin.Android tools:

这允许 Xamarin tools 将资源正确编译并嵌入到 APK 文件中。This allows the Xamarin.Android tools to properly compile and embed the resources in to the APK file. 如果由于某种原因而生成操作未设置为Android 资源,则将从 APK 中排除这些文件,尝试加载或访问资源的任何尝试都会导致运行时错误,应用程序将崩溃。If for some reason the Build Action is not set to Android Resource, then the files will be excluded from the APK, and any attempt to load or access the resources will result in a run-time error and the application will crash.

另外,请务必注意,尽管 Android 仅支持资源项的小写文件名,但包容性更多;它支持大写和小写文件名。Also, it's important to note that while Android only supports lowercase filenames for resource items, Xamarin.Android is a bit more forgiving; it will support both uppercase and lowercase filenames. 映像名称的约定是使用小写形式的下划线作为分隔符(例如,我的 _图像_名称 .png)。The convention for image names is to use lowercase with underscores as separators (for example, my_image_name.png). 请注意,如果使用破折号或空格作为分隔符,则无法处理资源名称。Note that resource names cannot be processed if dashes or spaces are used as separators.

将资源添加到项目后,可通过两种方式在应用程序中使用它们 – 以编程方式(在代码中)或 XML 文件。Once resources have been added to a project, there are two ways to use them in an application – programmatically (inside code) or from XML files.

以编程方式引用资源Referencing Resources Programmatically

若要以编程方式访问这些文件,将为它们分配唯一的资源 ID。To access these files programmatically, they are assigned a unique resource ID. 此资源 ID 是在名为 Resource的特殊类中定义的整数,可在Resource.designer.cs文件中找到,如下所示:This resource ID is an integer defined in a special class called Resource, which is found in the file Resource.designer.cs, and looks something like this:

public partial class Resource
{
    public partial class Attribute
    {
    }
    public partial class Drawable {
        public const int Icon=0x7f020000;
    }
    public partial class Id
    {
        public const int Textview=0x7f050000;
    }
    public partial class Layout
    {
        public const int Main=0x7f030000;
    }
    public partial class String
    {
        public const int App_Name=0x7f040001;
        public const int Hello=0x7f040000;
    }
}

每个资源 ID 都包含在与资源类型相对应的嵌套类中。Each resource ID is contained inside a nested class that corresponds to the resource type. 例如,将文件图标 .png添加到项目中时,Xamarin 更新了 Resource 类,并创建了一个名为 Drawable 的嵌套类,其中包含一个名为 Icon的常量。For example, when the file Icon.png was added to the project, Xamarin.Android updated the Resource class, creating a nested class called Drawable with a constant inside named Icon. 这允许在代码中将文件图标 .png作为 Resource.Drawable.Icon引用。This allows the file Icon.png to be referred to in code as Resource.Drawable.Icon. 不应手动编辑 Resource 类,因为对它所做的任何更改都将被 Xamarin 覆盖。The Resource class should not be manually edited, as any changes that are made to it will be overwritten by Xamarin.Android.

以编程方式引用资源时(在代码中),可以通过资源类层次结构访问这些资源,该层次结构使用以下语法:When referencing resources programmatically (in code), they can be accessed via the Resources class hierarchy which uses the following syntax:

[<PackageName>.]Resource.<ResourceType>.<ResourceName>
  • PackageName – 提供资源的包,并且仅当使用其他包中的资源时需要此包。PackageName – The package which is providing the resource and is only required when resources from other packages are being used.

  • ResourceType – 这是上面所述的资源类内的嵌套资源类型。ResourceType – This is the nested resource type that is within the Resource class described above.

  • 资源名称– 这是 XML 元素中的资源的文件名(不带扩展名)或 android: Name 属性的值。Resource Name – this is the filename of the resource (without the extension) or the value of the android:name attribute for resources that are in an XML element.

从 XML 引用资源Referencing Resources from XML

XML 文件中的资源可通过以下特殊语法来访问:Resources in an XML file are accessed by a following a special syntax:

@[<PackageName>:]<ResourceType>/<ResourceName>
  • PackageName – 提供资源的包,并且仅当使用其他包中的资源时需要此包。PackageName – the package which is providing the resource and is only required when resources from other packages are being used.

  • ResourceType – 这是资源类中的嵌套资源类型。ResourceType – This is the nested resource type that is within the Resource class.

  • 资源名称– 这是资源(不包含文件类型扩展名)的文件名,或者是 XML 元素中资源的 android:name 属性的值。Resource Name – this is the filename of the resource (without the file type extension) or the value of the android:name attribute for resources that are in an XML element.

例如,布局文件的内容main.axml如下所示:For example the contents of a layout file, Main.axml, are as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <ImageView android:id="@+id/myImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/flag" />
</LinearLayout>

此示例的ImageView需要一个名为 "标记" 的可绘制资源。This example has an ImageView that requires a drawable resource named flag. ImageView 将其 src 属性设置为 "@drawable/flag"。The ImageView has its src attribute set to @drawable/flag. 当活动开始时,Android 将在目录资源/可绘制的文件中查找名为 "标记" 的文件(文件扩展名可以是另一个图像格式,如 "标志 .jpg")并加载该文件并将其显示在 ImageView中。When the activity starts, Android will look inside the directory Resource/Drawable for a file named flag.png (the file extension could be another image format, like flag.jpg) and load that file and display it in the ImageView. 运行此应用程序时,它将如下图所示:When this application is run, it would look something like the following image:

已本地化 ImageView