Android 资源Android Resources
02/01/2018
本文内容
本文介绍了 Xamarin 中 Android 资源的概念,并介绍了如何使用这些资源。其中介绍了如何使用 Android 应用程序中的资源来支持应用程序本地化和多个设备,包括不同的屏幕大小和密度。This article introduces the concept of Android resources in Xamarin.Android and will document how to use them. It covers how to use resources in your Android application to support application localization, and multiple devices including varying screen sizes and densities.
概述Overview
Android 应用程序很少只是源代码。An Android application is seldom just source code. 通常有许多其他文件组成应用程序:视频、图像、字体和音频文件。There are often many other files that make up an application: video, images, fonts, and audio files just to name a few. 这些非源代码文件统称为资源,并在生成过程中进行编译,并打包为 APK 以便分发和安装到设备上:Collectively, these non-source code files are referred to as resources and are compiled (along with the source code) during the build process and packaged as an APK for distribution and installation onto devices:
资源为 Android 应用程序提供了若干优点:Resources offer several advantages to an Android application:
代码分离– 将源代码与图像、字符串、菜单、动画、颜色等分隔开来。当本地化时,此类资源可以很大的帮助。Code-Separation – Separates source code from images, strings, menus, animations, colors, etc. As such resources can help considerably when localizing.
面向多个设备– 提供对不同设备配置的更简单支持,而无需更改代码。Target multiple devices – Provides simpler support of different device configurations without code changes.
编译时检查– 资源是静态的,并编译到应用程序中。Compile-time Checking – Resources are static and compiled into the application. 这允许在编译时检查资源的使用情况,在这种情况下,可以轻松地捕获和更正错误,而不是在更难找到且更昂贵的时候运行。This allows the usage of the resources to be checked at compile time, when it will be easy to catch and correct the mistakes, as opposed to run-time when it is more difficult to locate and costly to correct.
当启动新的 Xamarin Android 项目时,将创建一个名为 "资源" 的特殊目录以及一些子目录:When a new Xamarin.Android project is started, a special directory called Resources is created, along with some subdirectories:
在上面的图像中,应用程序资源根据其类型组织到这些子目录中:图像将进入可绘制目录;视图位于布局子目录中,等等。In the image above, the application resources are organized according to their type into these subdirectories: images will go in the drawable directory; views go in the layout subdirectory, etc.
在上面的图像中,应用程序资源根据其类型组织到这些子目录中:图像将进入mipmap目录;视图位于布局子目录中,等等。In the image above, the application resources are organized according to their type into these subdirectories: images will go in the mipmap directory; views go in the layout subdirectory, etc.
可以通过两种方法在 Xamarin Android 应用程序中访问这些资源:以编程方式在代码中,并使用特殊的 xml 语法在 XML 中以声明方式。There are two ways to access these resources in a Xamarin.Android application: programmatically in code and declaratively in XML using a special XML syntax.
这些资源称为 "默认资源",并由所有设备使用,除非指定了更具体的匹配项。These resources are called Default Resources and are used by all devices unless a more specific match is specified. 此外,每种类型的资源可能有可选的备用资源,Android 可能会使用这些资源来定位特定设备。Additionally, every type of resource may optionally have Alternate Resources that Android may use to target specific devices. 例如,可能会提供资源以面向用户的区域设置、屏幕大小,或者设备是否旋转了90度(从纵向到横向)等。在上述每种情况下,Android 都将加载用于应用程序的资源,而无需开发人员进行任何额外的编码工作。For example, resources may be provided to target the user's locale, the screen size, or if the device is rotated 90 degrees from portrait to landscape, etc. In each of these cases, Android will load the resources for use by the application without any extra coding effort by the developer.
通过向包含给定资源类型的目录的末尾添加一个名为限定符的短字符串来指定备用资源。Alternate resources are specified by adding a short string, called a qualifier, to the end of the directory holding a given type of resources.
例如,资源/可绘制的 de将为设置为德语区域设置的设备指定映像,而资源/可绘制的将为设置为法语区域设置的设备保留图像。For example, resources/drawable-de will specify the images for devices that are set to a German locale, while resources/drawable-fr would hold images for devices set to a French locale. 在下图中,提供备用资源的示例如下所示:在运行同一应用程序时,只需更改设备区域设置:An example of providing alternate resources can be seen in the image below where the same application is being run with just the locale of the device changing:
本文全面介绍了如何使用资源,并涵盖了以下主题:This article will take a comprehensive look at using resources and cover the following topics:
Android 资源基础知识– 以编程方式使用默认资源,并以声明方式向应用程序添加资源类型(如图像和字体)。Android Resource Basics – Using default resources programmatically and declaratively, adding resource types such as images and fonts to an application.
设备特定配置– 支持应用程序中的不同屏幕分辨率和密度。Device Specific Configurations – Supporting the different screen resolutions and densities in an application.
本地化– 使用资源来支持应用程序可使用的不同区域。Localization – Using resources to support the different regions an application may be used.
相关链接Related Links