Android 开发指南 翻译4 Application Resources

时间:2022-03-12 17:54:57

Application Resources 应用系统资源

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.

你应该把你的资源从你的应用代码中隔离出来,一般可以单独维护它们。隔离的资源允许你提供支持特殊设备的可选资源,例如不同语言或屏幕大小。

Android 开发指南 翻译4 Application Resources

Figure 1. Two different devices, each using the default layout (the app provides no alternative layouts).两个设备使用都使用缺省资源

Android 开发指南 翻译4 Application Resources

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 the res/layout-land/ directory. Android automatically applies the appropriate resources by matching the device's current configuration to your resource directory names.

缺省res/layout目录的横向可选资源res/layout-land目录。

Android自动使用匹配设备当前配置的资源

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.

如何使用资源:从代码或从xml资源文件

Handling Runtime Changes

How to manage configuration changes that occur while your Activity is running.

Activity运行时,如何管理配置改变

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.
各种资源类型的参考资料。