Android 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

  安卓程序是Java程序語言編寫的。安卓SDK工具將編譯你的代碼、任何數據、資源文件一塊兒打包成APK:以.apk後綴名的存檔文件的一種Android包。一個APK文件包含全部Android程序的內容,全部使用Android的設備也是用APK文件來安裝Android應用的。android

Once installed on a device,each Android app lives its own security sandbox:web

  • 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:app

  • 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.

相關文章
相關標籤/搜索