Android 開發指南 翻譯4 Application Resources

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.

你應該把你的資源從你的應用代碼中隔離出來,一般可以單獨維護它們。隔離的資源允許你提供支持特殊設備的可選資源,例如不同語言或屏幕大小。

Figure 1. Two different devices, each using the default layout (the app provides no alternative layouts).兩個設備使用都使用缺省資源

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.
各種資源類型的參考資料。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章