It takes more than just code to build a great app. Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more.
它需要的不仅仅是代码,以打造成功的应用了。资源是额外的文件,您的代码使用静态内容,如位图,布局定义,用户界面字符串,动画指导,等等。
You should always externalize resources such as images and strings from your application code, so that you can maintain them independently. Externalizing your resources also allows you to provide alternative resources that support specific device configurations such as different languages or screen sizes, which becomes increasingly important as more Android-powered devices become available with different configurations. In order to provide compatibility with different configurations, you must organize resources in your project's res/
directory, using various sub-directories that group resources by type and configuration.
![](https://developer.android.com/images/resources/resource_devices_diagram1.png)
Figure 1. Two different devices, each using the default layout (the app provides no alternative layouts).
![](https://developer.android.com/images/resources/resource_devices_diagram2.png)
Figure 2. Two different devices, each using a different layout provided for different screen sizes.
For any type of resource, you can specify default and multiple alternative resources for your application:
- Default resources are those that should be used regardless of the device configuration or when there are no alternative resources that match the current configuration.
- Alternative resources are those that you've designed for use with a specific configuration. To specify that a group of resources are for a specific configuration, append an appropriate configuration qualifier to the directory name.
For example, while your default UI layout is saved in the res/layout/
directory, you might specify a different layout to be used when the screen is in landscape orientation, by saving it in theres/layout-land/
directory. Android automatically applies the appropriate resources by matching the device's current configuration to your resource directory names.
Figure 1 illustrates how the system applies the same layout for two different devices when there are no alternative resources available. Figure 2 shows the same application when it adds an alternative layout resource for larger screens.
The following documents provide a complete guide to how you can organize your application resources, specify alternative resources, access them in your application, and more:
-
Providing Resources
- What kinds of resources you can provide in your app, where to save them, and how to create alternative resources for specific device configurations. Accessing Resources
- How to use the resources you've provided, either by referencing them from your application code or from other XML resources. Handling Runtime Changes
- How to manage configuration changes that occur while your Activity is running. Localization
- A bottom-up guide to localizing your application using alternative resources. While this is just one specific use of alternative resources, it is very important in order to reach more users. Resource Types
- A reference of various resource types you can provide, describing their XML elements, attributes, and syntax. For example, this reference shows you how to create a resource for application menus, drawables, animations, and more.
你应该总是外部化资源,如从应用程序代码的图像和字符串,这样就可以独立维护他们。外部化你的资源,您还可以提供支持特定的设备配置,如不同的语言或屏幕尺寸,这变得更Android设备变得可利用不同的配置越来越重要的替代资源。为了提供具有不同配置的兼容性,您必须在项目的组织资源RES /
目录下,使用各种子目录,通过类型和配置组资源。
![](https://developer.android.com/images/resources/resource_devices_diagram1.png)
图1.两个不同的设备,每个都使用默认布局(应用程序提供了无可奈何的布局)。
![](https://developer.android.com/images/resources/resource_devices_diagram2.png)
图2.两个不同的设备,利用各自提供不同的屏幕大小不同的布局。
对于任何类型的资源,您可以指定默认和多个 替代资源,为您的应用:
- 默认资源是那些应而不管使用的设备配置或当存在匹配的当前配置,没有替代资源。
- 替代资源是那些你已经设计为与特定的配置使用。要指定一组资源是针对特定的配置,追加一个适当的配置预选赛的目录名。
例如,当你默认的UI布局保存在RES /布局/
目录下,你可以指定不同的布局,当屏幕处于横向使用,被保存在RES /布局土地/
目录下。通过Android的设备的当前配置匹配的资源目录名称会自动应用相应的资源。
图1示出系统如何适用于两个不同的设备相同的布局时没有可用的替代资源。图2显示了相同的应用程序时,它增加了对大屏幕的替代布局资源。
下列文件提供完整的指导你如何组织你的应用程序资源,指定替代资源,在应用程序中访问它们,以及更多:
-
提供资源
- 什么样的资源,你可以在你的应用程序,在那里保存它们,以及如何为特定设备配置替代资源提供。 访问资源
- 怎么样,无论是从应用程序代码或从其他XML资源引用,使用您所提供的资源。 处理运行时更改
- 如何管理您的活动运行时发生的配置更改。 本土化
- 自下而上指导使用替代资源本地化应用程序。虽然这只是一个替代资源的具体使用,这是为了吸引更多的用户非常重要。 资源类型
- 不同的资源类型,您可以提供,描述他们的XML元素,属性和语法的参考。例如,该参考展示了如何创建应用程序菜单,图形,动画和更多的资源。