Xamarin 中的Android资源及使用
Xamarin 中的Android 资源及使用
本文介绍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 应用程序只包含源码。通常还有很多组成应用程序的文件:视频、图像、音
频文件等等。总体上说,非源码文件都是资源,在生成过程和打包APK 的时候与源码一起
编辑,并同时安装到设备:
An Android application is seldom just source code. There are often many other files that make up
an application: video, images, and audio files just to name a few. 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 应用程序提供各种优势:
代码隔离 将代码与图像、字符、菜单、动画、颜色等隔离开。比如可以方便的 地化。
多设备部署 无需修改代码就可以简单的实现不同设备的配置支持。
编译时检查 资源是静态的,可编译到应用程序中。这样就可以在编译的时候检查资源,修
正错误,否则在运行时很难定位和修改错误。
当新创建一个Xamarin Android 项目,会创建一个叫做Resources 的目录,同时还有很多子
目录:
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.
When a new Xamarin.Android proj ect is started, a special directory called Resources is created,
along with some subdirectories:
上图所示,应用程序资源按类型组织到各个子目录中:图像放在drawable 目录;控件放在
layout 目录等等。
在Xamarin Android 应用程序中有两种方式访问这些资源:使用代码或声明式XML 。
这些资源叫做默认资源可用于所有设备,除非指定了设备匹配