首页 > 代码库 > Google 官方文档解析之——Application Fundamentals
Google 官方文档解析之——Application Fundamentals
Android apps are written in the java programming language.The Android SDK tools compile your code-along with any data and resource file-into an APK:an Android package,which is an archive file with an .apk suffix.One APK file contains all the contents of an Android app and is the file that Android-powered devices use to install the app.
安卓程序是Java程序语言编写的。安卓SDK工具将编译你的代码、任何数据、资源文件一起打包成APK:以.apk后缀名的存档文件的一种Android包。一个APK文件包含所有Android程序的内容,所有使用Android的设备也是用APK文件来安装Android应用的。
Once installed on a device,each Android app lives its own security sandbox:
- The Android operating system is a multi-user Linux system in which each app is a different user.
- By default,the system assigns each app a unique Linu user ID(the ID is used only by the system and is unknown to the app).The system sets permissions for all the files in an app so that only the user ID assigned to that app can access them.
- Each process has its own virtual machine(VM),so an app‘s code runs in isolation from other apps.
- by default,every app runs in its own Linux process.Android starts the process when any of the app‘s components need to be executed,then shuts down the process when it‘s no longer needed or when the system must revover memory for other apps.
一旦安装在一个设备上,任何一个Android 应用都会运行在一个它自己的安全砂箱中。
- Android操作系统是一个多用户的Linux内核操作系统,系统中每一个应用都被当做是一个不同的用户。
- 在默认情况下,系统分配每一个应用一个独一无二的Linux用户ID(这个ID只被系统使用,而对于应用来说它是不可知的)。系统为应用中的每一个文件都设置权限,以便只有分配给应用程序的用户ID才能访问他们。
- 每一个进程都有他自己的虚拟机,所以一个应用的代码将单独运行于其他应用程序。
- 一般来说,每一个应用程序都在他自己的Linux进程上运行。当应用程序的任意一个组件需要被运行的时候Android系统将启动应用程序对应的进程。而当不在需要应用程序或是系统必须为其他应用回收内存的时候系统将会关掉对应的进程。
In this way,the Android system implements the "principle of least privilege".That is ,each app,by default,has access only to the components that it requires to do its work and no more. This creates a very secure environment in which an app cannot access parts of the system for which it is not given permission.
这样,Android系统执行一种“最少特权原则”。那就是,每一个应用程序,一般来说,只有访问工作的组件的权限没有更多。这造成了一个非常安全的环境,每一个应用程序不能获得到系统没有赋予权限的部分。
Howerver there are ways for an app to share data with other apps and for an app to access system service :
- It‘s possible to arrange for two apps to share the same Linux user ID,in which case they are able to access each other‘s files.To conserve system resources,apps with the same user ID can also arrange to run in same Linux process and share the same VM(the apps must also be signed with the same certificate)
- An app can request permission to access device data such as the user‘s contacts,SMS messages,the mountable storage(SD card),camera,Bluetooth,and more.All app permissions must be granted by the user at intall time.
然而,应用程序也有许多方式和其他应用程序分享数据还有访问系统服务。
- 安排两个应用程序分享同一个Linux用户ID,在这种情况下,他们能访问彼此的文件。为了保存系统资源,拥有相同用户ID的应用程序也运行在同一个Linux进程中和共享虚拟机(应用程序必须被相同的证书签名)
- 一个应用程序可以为设备数据例如用户联系人、短消息、可安装存储(SD卡),照相机,蓝牙等等设备请求所有的权限必须被用户在安装的时候授予。
That convers the basics regarding how an Android app exists within the system.The rest of this document introduces you to:
- The core framework components that define your app.
- The manifest file in which you declare components and require device feature for you app.
- Resources that are separate from the app code and allow your app to gracefully optimize its behavior for a variety of device configurations.
这些覆盖了一些基础问题,一个Android应用如何在系统中存在。剩下的文档将向你介绍:
- 定义你的应用的核心框架组件 。
- 声明应用所需的组件和设备特性的清单文件。
- 独立于应用程序代码的资源允许你的程序优雅的优化其行为为一个多样化的设备配置。
App Components
App components are the essential building blocks of an Android app.Each component is a different point through which the system can enter your app.Not all components are actual entry points for the user and some depend on each other,but each one exist as its own entity and plays a specific role-each one is unique building block that helps define your app‘s overall behavior.
There are four different types of app components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.
Here are four types of app components:
App组件是Android应用程序的基本的构造模块。每一个组件都是不同的点,系统能进入你的应用程序。不是所有的组件是真正的用户进入点,有一些也互相依赖,但是每一个都作为自己的实体存在而且发挥一个特殊的作用——每一个都是独一无二的建造块帮助定义你的应用程序的总体行为。
有四个不同类型的应用程序组件。每一个类型为一个明显的目的服务,并且有一个明显的生命周期,定义了组件怎么被创建和销毁。
这里有四种类型的应用程序组件:
Activities
An activity represents a single screen with a user interface.For example,an email app might have one activity that shows a list of new emails,another activity to compose an email,and another activity for reading emails.Although the activities work together to form a cohesive(紧密结合的) user experience in the email app,each one is independent of the others.As such,a different app can start any one of these activities(if the email app allows it). For example,a camera app can start the email app that composes new mail,in order for the user to share a picture.
An activity is implemented as a subclass of Activity and you can learn more about it in the Activities developer guide.
一个activity代表用户界面的单个屏幕。例如,一个电子邮件应用程序也许有一个activity显示一个新邮件列表,另一个activity构成一封电子邮件,另一个activity来阅读电子邮件。尽管这些activity在电子邮件应用程序中一起工作构成一个紧密结合的用户经验,每一个activity对于其他的来说都是独立的。同样的,一个不同的应用程序能启动他们中任何一个activity(只要Email app允许)。例如,一个照相app能启动Email app中的新建邮件的activity,为了用户分享一张图片。
Services
A service is a component that runs in the background to perform(执行) long-running operations or to perform work for remote processes.A service does not provide a user interface.For example,a service might play music in the background while the user is in a different app,or it might fetch data over the network without blocking user interaction with an activity.Another component,such as an activity,can start the service and let it run or bind to it in order to interact with it.
A service is implemented as a subclass of Service an you can learn about it in the Services developer guide.
一个service是一个组件运行在后台为了执行耗时操作或者是为远程进程工作。一个service不提供用户界面。例如一个service可以在后台播放音乐当用户在另一个不同的app中,或者是在一个activity中不阻塞用户界面获取网络数据。一个组件,比如activity可以启动service让他运行或者是绑定service为了和它互相起作用。
Content providers
A content provider manages a shared set of app data.You can store the data in the file system,an SQLite database,on the web,or any other persistent storage location your app can access.Through the content provider,other apps can query or even modify the data(if the content provider allows it).For example,the Android system provides a content provider that manages the user‘s contact information.As such,any app with the proper permissions can query part of the content provider(such as ContactsContract.Data)to read and write information about a particular person.
Content providers are also useful for reading and writing data that is private to your app and not shared.For example,the Note Pad sample app uses a content provider to save notes.
A content provider is implemented as a subclass of ContentProvider and must implement a stadard set of APIs that enable other apps to perform(执行) transactions.For more information,see the Content Providers developer guide.
一个content provider 管理一个共享的app数据集合。你可以存储数据在文件系统、一个SQLite数据库、网络中,或是其他任何你的应用可以访问的持久化存储地址。通过content provider,其他应用可以查询或者甚至修改这些数据(如果content provider允许)。例如Android系统提供一个content provider 管理用户的联系人信息。同样的,任何一个有适当权限的app可以查询联系人的content provider(例如ContactsContract.Data)来读或是写一个详细的联系人的信息。
content providers 在读写你应用中未被共享的私有数据也是有用处的。例如Note Pad应用中用一个content provider 来保存笔记。
一个content provider 是一个实现ContentProdider的子类,必须实现一套API标准确保其他应用执行事务。查看更多信息,请看Content Providers开发指南。
Broadcast reveivers
A broadcast receiver is a component that responds to system-wide broadcast announcement.Many broadcasts originate from the system-for example,a bropadcast announcing that the screen has turned off,the battery is low,or a picture was captured.Apps can also initiate broadcasts-for example,to let other apps know that some data has been downloaded to the device and is available for them to use.Although broadcast receivers don‘t display a user interface,they may create a status bar notification to alert the user when a broadcast event occurs.More commonly,though,a broadcast receiver is just a "gateway" to other commponents and is intended to do a very minimal amount of work.For instance,it might initiate a service to perform some work based on the event.
A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object.For more information,see BroadcastReceiver class.
一个broadcast receiver是一个响应系统范围的广播通知组件。许多广播是系统引起的,例如屏幕旋转的广播通知,电量过低或是一张图片被捕获。应用程序也能初始化广播,例如让其他应用知道一些数据被下载到设备而且对他们来说是可用的。尽管broadcast receivers不提供用户界面,但当有一个广播事件发生的时候他们能创建一个状态栏通知来提示用用户。更常见的是broadcast receiver对于其他组件来说仅仅是一个“通道”,他只做非常少的工作。例如他也许实例化了一个服务来执行一些基于这个事件的工作。
broadcast receiver 是一个实现了BroadcastReceiver的子类,而且每一个broadcast都被作为一个Intent对象来交付。想要更多关于broadcast receiver的信息,请看BroadcastReceiver类。
A unique aspect of the Android system design is that any app can start another app‘s component.For example,if you want the user to capture a photo with the device camera,there‘s probably another app that does that and your app can use it,instead of developing an activity to capture a photo yourself.You don‘t nedd to incorporate or even link to the code from the camera app.Instead,you can simply start the activity in the camera app that captures a photo.When complete,the photo is even returned your app so you can use it.To the user,it seems as if the camera is actually a part of your app.
Android系统的一个独特的方面是任何应用程序都可以启动其他应用的组件。例如,如果你想要用户用照相机拍一张照片,很有可能其他应用程序做了这个功能而你的应用可以调用它而不是开发一个Activity用来自己照相。你不需要合并或是从相机app中链接代码。替代的是,你能简单的启动相机app中的activity来拍摄照片。当照完照片的时候,照片会返回到你的应用然后你就可以使用了。对于用户来说,就好像照相机就是你应用程序的一部分一样。
When the system starts a component,it starts the process for that app(if it‘s not already running)and instantiates the classes needed for the component.For example,if your app starts the activity in the camera app that captures a photo,that activity runs in the process that belongs to the camera app,not in your app‘s process.Therefore,unlike apps on most other systems,Android apps don‘t have a single entry point(there‘s no main() function,for example).
当系统启动一个组件的时候,系统将会为所在的app启动进程(如果没有正在运行)还会实例化组件所需要的类。例如,如果你的app启动相机app的照相activity,拍照activity运行在相机app所在的进程中,而不是你的app所在的进程。因此,不像大多数其他操作系统中的应用程序,Android 应用程序没有一个单独的程序入口(没有main()方法,例如)。
Because the system runs each app in a separete process with file permissions that restrict access to other apps,your app cannot directly activate a component from other app.The Android system,however,can.So,to activate a component in another app,you must deliver a message to the system that specifies your intent to start a particular component.The system then activates the component for you.
因为系统运行应用程序在文件权限下的分开的进程中,进入其他应用有限制条件,所以你的应用程序不能直接激活其他app的组件。但是Android系统可以。所以为了激活其他应用中的组件,你必须向系统发送一条消息并且指定你的intent来启动一个特别的组件。然后系统将会为你激活相应的组件。
Activity Components
Three of the four component types-activities,services,and broadcast receivers-are activated by an asynchronous message called an intent.Intents bind individual componnets to each other at runtime(you can think of them as the messengers that request an action from ohter components),whether the component belongs to your app or anohter.
四大组件中有三个-activity、service、broadcast receiver,都是被一个叫做intent的异步消息激活的。intent在运行时绑定到独特的组件(你可以吧他们当做从其他组件请求动作的送信者),不管这个组件是不是属于你的应用程序。
An intent is created with an Intent object,which defines a message to activate either specific(特殊的) component or a specific type of component-an intent can be either explicit(明确的) or implicit,respectively.
定义一个信息激活一个特殊组件或是特殊类型组件的intent是被一个Intent对象创建的,一个intent是明确的或是暗示的。
For activities and services,an intent defines the action to perform(for example,to "view"or"send"something)and may specify the URI of the data to act on(among other things that the component being started might need to know).For example,an intent might convey a request for an activity to show an image or to open a web page.In some cases,you can start an activity to receive a result,in which case(在这种情况下),the activity also returns the result in an Intent(for example,you can issue an intent to let the user pick a personal contact and have it returned to you-the return intent includes a URI pointing to the chosen contact).
对于activity和service来说,一个intent定义要执行的动作(例如“查看”或“发送”东西)还有可能指定将要对其起作用的数据的URI(除了别的之外将要启动的组件需要知道)。例如一个intent可能为一个Activity传递一个请求来显示一张图片或是打开一个网页。在某种情况下,你可以开启一个Activity来接收一个结果,在这种情况下,这个Activity也在intent中返回结果(例如你可以发布一个intent来让用户选择一个联系人然后把它返回给你,这个返回的intent包括一个指向选择的联系人的URI指针)。
For broadcast receivers, the intent simply defines the announcement being broadcast (for example, a broadcast to indicate the device battery is low includes only a known action string that indicates "battery is low").
对于broadcast receiver来说,intent简单的定义了被广播的通知(例如一个指明设备电量过低的广播仅仅包括一个动作字符串指明“电量过低”)。
The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver. The content resolver handles all direct transactions with the content provider so that the component that‘s performing transactions with the provider doesn‘t need to and instead calls methods on the ContentResolver object. This leaves a layer of abstraction between the content provider and the component requesting information (for security).
其他的组件类型,content provider不是被intent激活的。当被一个叫做ContentResolver的请求作为目标的时候才被激活。内容解析器与content provider直接处理事务,这样组件与conent provider就不需要执行交易了,而是调用ContentResolver对象上的方法。在content provider和需要查询信息的组件之间就离开了一层抽象(在安全性上来说)。
There are separate methods for activating each type of component:
- You can start an activity (or give it something new to do) by passing an Intent to startActivity() or startActivityForResult() (when you want the activity to return a result).
- You can start a service (or give new instructions to an ongoing service) by passing an Intent to startService(). Or you can bind to the service by passing an Intent to bindService().
- You can initiate a broadcast by passing an Intent to methods like sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().
- You can perform a query to a content provider by calling query() on a ContentResolver.
激活每一种类型的组件有不同的方法:
- 你可以启动一个activity(或是给他些新的东西做)通过传递一个Intent到startActivity()或是startActivityForResult()方法(当你想要activity返回一个结果)
- 你可以启动一个service(或是给新的指令到一个不间断的service)通过传递一个Intent到startService()方法。或者是你可以绑定service通过传递Intent到bindService().
- 你可以通过传递一个Inent到像sendBroadcast()、sendOrderedBroadcast()或是sendStickyBroadcast()的方法来初始化一个broadcast.
- 你可以通过在ContentResolver中调用query()方法来执行查询到content provider.
For more information about using intents, see the Intents and Intent Filters document. More information about activating specific components is also provided in the following documents: Activities, Services, BroadcastReceiver and Content Providers.
查看更多关于使用intent,请查看Intents and Inent Filters文档。更多信息关于激活特殊组件也被提供在以下文档:Activities,Services,BroadcastReceiver和Content Providers.
The Manifest File
Before the Android system can start an app component,the system must know that the component exists by reading the app‘s AndroidManifest.xml file(the "mainfest"file).Your app must declare all its componet in this file,which must be at the root of the app project drectory.
在Android系统在启动一个应用程序组件之前,系统必须得通过读取应用的AndroidManifest.xml文件("清单"文件)来知道存在的组件。你的应用程序必须在这个文件中定义所有的用到的组件,清单文件也必须在应用程序项目的根目录下。
The manifest does a number of things in addition to declaring the app‘s components, such as:
- Identify any user permissions the app requires, such as Internet access or read-access to the user‘s contacts.
- Declare the minimum API Level required by the app, based on which APIs the app uses.
- Declare hardware and software features used or required by the app, such as a camera, bluetooth services, or a multitouch screen.
- API libraries the app needs to be linked against (other than the Android framework APIs), such as the Google Maps library.
清单文件除了定义应用程序组件之外还有有一些东西,例如:
- 定义应用程序需要的用户权限,例如网络使用权或是读取用户联系人的权限
- 定义应用程序需要使用基于的最小API等级
- 定义应用程序使用或是需要的硬件和软件特性,例如相机、蓝牙服务或是多点触控屏幕。
- 应用程序需要链接的API包(除了Android framework APIs),例如Google Maps library.
Declaring components
The primary task of the manifest is to inform the system about the app‘s components. For example, a manifest file can declare an activity as follows:
清单的主要任务是告诉系统有哪些应用程序组件。例如,一个清单文件可以定义一个activity如下:
<?xml version="1.0" encoding="utf-8"?><manifest ... ><application android:icon="@drawable/app_icon.png" ... ><activity android:name="com.example.project.ExampleActivity"android:label="@string/example_label" ... ></activity>...</application></manifest>
In the <application> element, the android:icon attribute points to resources for an icon that identifies the app.In the <activity> element, the android:name attribute specifies the fully qualified class name of the Activity subclass and the android:label attributes specifies a string to use as the user-visible label for the activity.
在<application>元素中,"android:icon"属性指定了资源中的一个图标来识别应用程序。在<activity>元素中,"android:name"属性指明了整个限制性的Activity子类的类名还有"andorid:label"元素指明了一个为activity用作用户可见的标签字符串。
You must declare all app components this way:
- <activity> elements for activities
- <service> elements for services
- <receiver> elements for broadcast receivers
- <provider> elements for content providers
Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run. However, broadcast receivers can be either declared in the manifest or created dynamically in code (as BroadcastReceiver objects) and registered with the system by calling registerReceiver().
你在源文件中包含的Activitiy、service和conent provider而没有在清单文件中定义在系统中是不可见的,因此,不能运行。然而,broadcast receiver既能在清单文件中定义或是动态的在代码中被创建(作为BroadcastReciver对象)通过调用registerReceiver()方法来注册。
For more about how to structure the manifest file for your app, see The AndroidManifest.xml File documentation.
Declaring component capabilities
As discussed above, in Activating Components, you can use an Intent to start activities, services, and broadcast receivers. You can do so by explicitly(明确地) naming the target component (using the component class name) in the intent. However, the real power of intents lies in the concept of implicit intents. An implicit intent simply describes the type of action to perform (and, optionally 随意地, the data upon which you’d like to perform the action) and allows the system to find a component on the device that can perform the action and start it. If there are multiple components that can perform the action described by the intent, then the user selects which one to use.
如上所述,在激活组件上,你可以用一个Intent来启动activites,services,还有broadcast receivers.你可以在intent中通过明确的给目标组件命名(用组件的类名)来激活组件。然而,intent的真正强大的地方在于隐式的intent概念。一个隐式的intent简单的描述了要执行动作的类型(还有你想要执行动作上的数据),允许系统在设备上找到一个能执行这个动作的组件并且启动它。如果这里有多个组件能够执行被intent描述的动作,然后用户可以选择用哪一个区执行它。
The way the system identifies the components that can respond to an intent is by comparing the intent received to the intent filters provided in the manifest file of other apps on the device.
系统可以识别对某intent作出响应的组件方式是通过将接受到的intent和设备中其他程序的manifest文件的intent filters进行比较来实现的。
When you declare an activity in your app‘s manifest, you can optionally include intent filters that declare the capabilities of the activity so it can respond to intents from other apps. You can declare an intent filter for your component by adding an <intent-filter> element as a child of the component‘s declaration element.
当你在你的应用程序清单中定义一个activity时,你可以随意的包含intent filters,这些intent filters定义了activity的能力它能响应其他应用中的intent.你可以为你的组件定义一个intent filter通过添加一个<intent-filter>元素作为组件声明元素的子元素。
For example, if you‘ve built an email app with an activity for composing a new email, you can declare an intent filter to respond to "send" intents (in order to send a new email) like this:
例如,如果你已经构造了一个电子邮件应用程序,它有一个写Email的Activity,你可以定义一个intent filter来回应"发送"intents(为了发送一个新邮件)像这样:
<manifest ... >...<application ... ><activity android:name="com.example.project.ComposeEmailActivity"><intent-filter><action android:name="android.intent.action.SEND" /><data android:type="*/*" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity></application></manifest>
Then, if another app creates an intent with the ACTION_SEND action and pass it to startActivity(), the system may start your activity so the user can draft and send an email.
然后,如果其他应用程序创建一个包含ACTION_SEND action的intent然后把它传给startActivity()方法,系统启动你的activity然后用户可以起草并发送一个email.
For more about creating intent filters, see the Intents and Intent Filters document.
Declaring app requirements
There are a variety of devices powered by Android and not all of them provide the same features and capabilities. In order to prevent your app from being installed on devices that lack features needed by your app, it‘s important that you clearly define a profile for the types of devices your app supports by declaring device and software requirements in your manifest file. Most of these declarations are informational only and the system does not read them, but external services such as Google Play do read them in order to provide filtering for users when they search for apps from their device.
有各种各样的设备是Android设备,但所有的设备没有提供相同的特性和性能。为了防止你的应用在缺少你应用需要的特性上的设备上安装,你清楚的定义一个你应用支持设备的简况通过在manifest文件中声明所需要的设备和软件是很重要的。大部分的声明仅仅是声明的,系统并不去读它,但是外部服务例如Google Play会读取它,为了当用户为他们的设备搜索应用程序的时候提供过滤器。
For example, if your app requires a camera and uses APIs introduced in Android 2.1 (API Level 7), you should declare these as requirements in your manifest file like this:
例如,如果你的app需要一个照相机并且需要Android2.1(API Level 7)的APIs,你应当在你的清单文件中像这样声明:
<manifest ... ><uses-feature android:name="android.hardware.camera.any"android:required="true" /><uses-sdk android:minSdkVersion="7" android:targetSdkVersion="19" />...</manifest>
Now, devices that do not have a camera and have an Android version lower than 2.1 cannot install your app from Google Play.
However, you can also declare that your app uses the camera, but does not require it. In that case, your app must set the required attribute to "false" and check at runtime whether the device has a camera and disable any camera features as appropriate.
现在呢,没有照相机或是Android版本低于2.1的设备将不能从Google Play中安装你的设备。
然后,你也能定义你的应用程序使用照相机,但是不需要照相机。在这样的情况下,你的应用程序必须设置需要的属性为"false"然后检查运行时是否设备有照相机然后视情况而定的将相机属性设为不可用。
More information about how you can manage your app‘s compatibility with different devices is provided in the Device Compatibility document.
App Resources
An Android app is composed of more than just code—it requires resources that are separate from the source code, such as images, audio files, and anything relating to the visual presentation of the app. For example, you should define animations, menus, styles, colors, and the layout of activity user interfaces with XML files. Using app resources makes it easy to update various characteristics of your app without modifying code and—by providing sets of alternative resources—enables you to optimize your app for a variety of device configurations (such as different languages and screen sizes).
一个Android 应用程序不仅仅是代码组成的,他也需要与代码分隔开来的资源,例如图像,媒体文件,还有任何一些App相关的可视图像。例如,你可以使用XML文件来定义动画,菜单,样式,颜色还有用户接口的activity。用app资源使更新你应用的的特征变得很简单,不用修改源代码(通过提供供选择的资源集合)能够使你为不同的设备配置优化你的App(例如不同的语言和屏幕尺寸)。
For every resource that you include in your Android project, the SDK build tools define a unique integer ID, which you can use to reference the resource from your app code or from other resources defined in XML. For example, if your app contains an image file named logo.png (saved in the res/drawable/ directory), the SDK tools generate a resource ID named R.drawable.logo, which you can use to reference the image and insert it in your user interface.
为每一个你在Android项目中包含的资源,SDK创建工具定义了独一无二的ID,你可以在你的代码中引用资源或是XML其他定义的资源。例如,如果你的App包含了一个图像文件叫做logo.png(保存在res/drawable/文件夹中),SDK工具生成一个资源ID叫做R.drawable.log,你可以用使用它来引用图像还可以在你的用户界面中插入这张图像。
One of the most important aspects of providing resources separate from your source code is the ability for you to provide alternative resources for different device configurations. For example, by defining UI strings in XML, you can translate the strings into other languages and save those strings in separate files. Then, based on a language qualifier that you append to the resource directory‘s name (such as res/values-fr/ for French string values) and the user‘s language setting, the Android system applies the appropriate language strings to your UI.
提供将资源从你的源代码中分隔开来最重要的方面之一是为你提供了为不同设备配置提供了可选择的资源。例如通过在XML定义UI字符串,你可以将字符串翻译为其他语言然后保存那些字符串在分开的文件中。然后基于一种语言的限定你可以扩展资源文件夹的名字(例如res/values-fr/为法语字符串资源)和用户语言设置,Android系统将为你的UI请求适当的语言字符串。
Android supports many different qualifiers for your alternative resources. The qualifier is a short string that you include in the name of your resource directories in order to define the device configuration for which those resources should be used. As another example, you should often create different layouts for your activities, depending on the device‘s screen orientation and size. For example, when the device screen is in portrait orientation (tall), you might want a layout with buttons to be vertical, but when the screen is in landscape orientation (wide), the buttons should be aligned horizontally. To change the layout depending on the orientation, you can define two different layouts and apply the appropriate qualifier to each layout‘s directory name. Then, the system automatically applies the appropriate layout depending on the current device orientation.
Android为你的可选资源支持许多不同的限定。修饰是你的资源文件夹的一个简短的字符串,为了给那些需要用到的资源定义设备配置。再如,你可以根据设备屏幕方向和大小为你的Activity创建不同的layouts。比如,当设备屏幕是纵向方向的(高的),你可以想要一个拥有按钮纵向排列的布局,但当屏幕是横向排列(宽的),按钮应该按照横向排列。根据方向改名布局,你可以定义两个不同的布局,然后申请适当的限定给不同的布局文件夹名字。(qualifier原来是资源文件中的文件夹名)。然后系统根据当前设备方向申请适当的布局。
For more about the different kinds of resources you can include in your application and how to create alternative resources for different device configurations, read Providing Resources.