码迷,mamicode.com
首页 > 移动开发 > 详细

安卓官方开发文档API GUIDE(1.1)Application Fundamentals 应用基础

时间:2014-11-26 11:34:27      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   io   ar   color   os   使用   sp   

 Application Fundamentals
Android apps are written in the Java programming language. The Android SDK tools compile your code—along with any data and resource files—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 package(安卓包),他是一个以.apk为后缀的档案文件。一个APK文件包括安卓app的所有内容,设备通过这个文件来安装安卓应用。
 Once installed on a device, each Android app lives in its own security sandbox(沙箱): 
一旦安卓应用被安装到你的设备,每一个安卓app都在自己的安全沙箱中存活。
? The Android operating system is a multi-user Linux system in which each app is a different user.
? 安卓操作系统是一个多用户的linux系统,每一个应用都是一个不同的用户。
? By default, the system assigns each app a unique Linux 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. 
? 默认,操作系统给每一个应用分配了独一无二的linux用户 ID(这个ID是操作系统使用的,而app不知道这个ID)
? Each process has its own virtual machine (VM), so an app‘s code runs in isolation from other apps.
? 每一个进程都有自己的虚拟机(VM),这就保证了一个应用的代码可以和另一个应用的代码独立运行,互不干扰。
? 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 recover memory for other apps.
? 默认,每一个应用都在自己的Linux进程下运行。当应用的组件需要被执行时,安卓系统运行这个进程。如果应用程序长时间不被使用,或者系统需要为另一个应用程序获得更多的运行空间时,将结束这个进程。
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.
因此,安卓系统实现最小特权的原则(在完成某种操作时所赋予网络中每个主体(用户或进程)必不可少的特权" 。你需要多少权限我就给你多少,多一丢丢我都不给你,你来骂我啊。)每一个app默认只有在需要执行他的工作的时候操作自己的组件。这就创建了一个非常安全的运行环境,因为一个app不能操作没有给定权限的系统部分。
However, there are ways for an app to share data with other apps and for an app to access system services:
但是,app之间需要共享数据而且app需要使用系统底层的服务。这该咋办呢?看看下面吧:
? 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 the same Linux process and share the same VM (the apps must also be signed with the same certificate).
? 安排两个app去共享同一个Linux 用户ID,这样就他们就可以访问操作对方的文件了。为了维护系统的资源,这两个app需要使用同一个用户ID来运行到同一个Linux进程下,并且通过同一个虚拟机来共享。但是,他们(apps)需要用同一个证书签名。
? 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 install time.
? 一个app可以请求一个权限,去访问操作设备数据,如联系人,SMS(Short Message Service短信服务)信息,可安装的存储设备(SD卡),相机,蓝牙等。所有的app权限需要在用户安装时由用户授予。(老罗说:这经常出现在我们安装应用的时候,经常会提示我们你安装的应用设计一下敏感权限,你可以不授予他。如使用2G/3G,wifi,开机自启动)。
That covers 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.
? 使用核心框架组件来定义你的app
? The manifest file in which you declare components and required device features for your app.
? 使用mainfest(清单文件)文件来声明组件并且要求设备适应你的app.
? Resources that are separate from the app code and allow your app to gracefully optimize its behavior for a variety of device configurations.
? App源代码中被分离的出资源 如何允许你的app最优的在多种设备配置下适配。
App Components
App组件
________________________________________
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 exists as its own entity and plays a specific role—each one is a unique building block that helps define your app‘s overall behavior.
App组件是建立一个安卓应用必须的部分。每一个组件都可以是一个操作系统执行app的起始点。不是所有的组件都是实际的切入点,(有的需要从其他组件来切入)但是他们作为实体存在,并且扮演着特殊的角色—每一个组件都是独一无二的必要成分,他们可以帮助你来定义的app的全部功能。
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.
一共有四种app组件。每一种都明确的目的,有明确的生命周期(何时生,何时死)
Here are the four types of app components:
下面详细介绍:
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 activity in the email app that composes new mail, in order for the user to share a picture.
一个Activity代表了用户看到的一个界面。比如说,一个邮件app可能会有一个activity来显示新邮件列表,另一个activity用来编写邮件,另一个用来读取邮件。虽然许多activity一起工作,在邮件app形成了一个流畅的用户体验。但是每一个activity都是独立的。比如说,不同的app可以启动邮件app中的一些activity(如果邮件app允许他这么做)。一个相机应用可以在启动邮件app中的编辑新邮件的activity,这就方便了用户来分享照片。
An activity is implemented as a subclass of Activity and you can learn more about it in the Activities developer guide.
一个Activity 通过继承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. 
一个Service是一个运行在系统后台的组件,实现运行时间很长的操作或者去和远程的进程进行通信。一个service不提供用户接口(user interface 即我们常说的UI)。比如说,当用户在操作其他的app时,一个service实现了在后台播放音乐的功能。再比如,service在后台运行通过网络来获取远程数据,不在app中阻塞用户接口(UI)。另外的组件,比如activity可以启动一个service ,并且与之进行绑定实现相互作用。
A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.
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.
一个内容提供者管理着app数据的共享方式。 你可以将数据存储在文件系统中,数据库中(SQLite),网络中,或者其他稳定的app可以访问的存储地址。虽然有内容提供者,但是其他的app可以查询甚至更改数据(如果content provider允许app这么做)。比如说,一个安卓操作系统提供了一个内容提供者来管理联系人的信息。同样的,一个有适当许可的app可以查询内容提供者的一部分(比如ContactsContract.Data)来读取或者编写某一个联系人的详细信息。
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.
在读取,编辑你的app私有的不共享的数据时,内容提供者通常也是有用的。比如,note pad的例子中使用内容提供者来保存数据。
A content provider is implemented as a subclass of ContentProvider and must implement a standard set of APIs that enable other apps to perform transactions. For more information, see the Content Providers developer guide.
一个内容提供者通过继承ContentProvider而且需要实现标准的API设置来确保其他的app实现这个事务。
Broadcast receivers 广播接收者
A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast 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 components 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. 
一个Broadcast Receiver 是一个回应操作系统广播公告的组件。许多广播是由操作系统发起的,比如说一个广播宣布屏幕休眠,电池电量低,或者拍了一张照片。App也可以初始一个广播,比如说,让其他的app知道设备下载了一些数据,通知他们可以使用这些数据了。即使,Broadcast Receiver 不展示用户接口,但是他们可以创建一个状态栏的通知,从而提醒用户广播开始了。通常,一个Broadcast Receiver 对其他组件来说只是一个通道,而且他做了非常少的工作。比如说,他可以初始化一个服务,用来完成一些工作。
A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object. For more information, see the BroadcastReceiver class.
Broadcast Receiver 通过继承Broadcast Receiver 来实现,不同的广播以intent 方式进行广播。
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 need 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 to your app so you can use it. To the user, it seems as if the camera is actually a part of your app.
安卓操作系统一个独特的特点就是:任何app都可以启动其他app的组件。比如说,如果你的app想让用户使用摄像头来拍摄照片,那你的app就可以使用一个有这个功能的另外一个app,而不用自己开发一个activity去拍照。你不必包含或者链接相机应用的代码,你只需启动相机app中的拍照activity。当拍摄完成后,照片传到你的app,这样你就可以使用了。对用户来说,这个相机实际上是你app的一部分。
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进程中。因此,和其他操作系统中的应用不同的是,安卓app没有一个单一的入口(没有主函数)
Because the system runs each app in a separate process with file permissions that restrict access to other apps, your app cannot directly activate a component from another 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在不同的进程中运行,他们通过文档权限去限制其他的app访问操作。你的app不能直接激活一个其他app的组件。但是,安卓操作系统可以。要激活其他app的组件,你必须传递一个信息告知操作系统,然后操作系统使用你的intent(意图)启动指定的组件。这样操作系统就帮你启动了你需要的组件。
Activating Components 激活组件
Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an intent. Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your app or another.
4大组件中的3个 activity ,service,broadcast receiver 都通过一个叫做intent的异步消息激活。Intent 绑定独立的组件(可以是你app的或者其他app的),在他们运行时进行切换。你可以认为他是一个消息传递者,他请求其他的组件。
An intent is created with an Intent object, which defines a message to activate either a specific component or a specific type of component—an intent can be either explicit or implicit, respectively.
一个intent通过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来定义表现动作(可以启动一个“view(视图)”或者“send(发送)”)。Intent也可能指定启动URI(Web上可用的每种资源 -HTML文档、图像、视频片段、程序等 - 由一个通用资源标识符(Uniform Resource Identifier, 简称"URI")进行定位。)数据来展示页面。比如说,intent可以传送请求给一个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 Resolver 操纵着content provider 的所有事务,因此这个表现该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).
? 你也使用Intent来调用startActivity()或者startActivityForResult(),从而启动一个activity或者让一个activity做其他的事情(就像让相机app打开照相功能)。
? 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().
? 你可以通过传递一个intent到startService(),从而启动服务(或者是给正在运行服务其他的新指令)
? You can initiate a broadcast by passing an Intent to methods like sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().
? 你可以通过传递一个intent到sendBroadcast(), sendOrderedBroadcast(), 或者sendStickyBroadcast()从而初始化广播。
? You can perform a query to a content provider by calling query() on a ContentResolver.
? 你可以通过在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.
关于使用intents的详细信息,请看Intents and Intent Filters 文档。在后面的文档中,也有一些关于激活某个组件的信息Activities, Services, BroadcastReceiver and 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 "manifest" file). Your app must declare all its components in this file, which must be at the root of the app project directory.
在Android系统开启一个应用组件之前,系统必须通过读取AndroidManifest.xml文件来知道组件的存在.你的应用必须把它所有的组件声明在这个文件中,并且必须在应用工程的根目录下.
The manifest does a number of things in addition to declaring the app‘s components, such as:
这个manifest文件除了声明组件外,还处理了许多其他的事情,比如:




? 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.
? 声明应用支持的最小API Level,通过 app使用的API来确定。
?
? 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),比如Google Maps library
? And more
? 等等
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: 
manifest文件的主要任务是通知操作系统定义的应用的组件,比如,一个manifest可以这样声明一个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.
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
在<application> 元素中,android:icon属性指定app的图标
在<activity> 元素中的,android:name 属性,指定Activity子类的完全类名名,android:label 属性,为activity指定一个用户可见的标签。 你必须这样声明 所有应用的组件:
? <activity> 声明活动的元素
? <service> 声明服务的元素
? <receiver> 声明广播接收者元素
? <provider> 声明内容提供者元素
?
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().
For more about how to structure the manifest file for your app, see The AndroidManifest.xml File documentation. 
在你代码中包含的,Activites,services和内容提供者,若没有在manifest中声明,对系统来说是不可见的,永远不会运行。但是,广播接收者即可以在manifest中声明,也可以在代码中动态创建(做为BroadcastReceiver对象)并且通过registerReceiver()方法向系统注册。
了解manifest文件的详细构建过程,请看The AndroidManifest.xml File文档


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.
就如在上面所讨论的,你可以用一个被激活的Activityt通过使用Intent启动activities,services和broadcast  receiver.你也可以在intent中显式的指定目标组件(使用组件类名)。然而,intent真正强大的是它的intent action.(动作)。通过使用intent动作,你只须简单的描述你要执行的action类型,(并且,可选的与执行动作有关的数据),并且允许系统在设备上找到一个组件,这样就可以执行那个动作并启动它。如果有多个组件可以执行intent指定的action,那么需要用户选择执行哪一个。


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.


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.
通过比较设备上的其他应用的manifest文件上的intent filters与接收到的intent.系统确定那个组件可以响应一个intent. 
当你在你的应用的manifest中声明一个组件时,你可以选择包括intent filters(意图过滤器),来指定组件的功能,以让其能响应其他应用的intents.你可以加一个组件声明的元素的子元素<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应用中,新建email的一个activity可能在它的manifest 中声明了一个意图过滤器,以便能响应”send”意图(为了发送邮件)。然后,在你的应用中的一个activity,创建了一个带有”send” ACTION_SEND的意图,.当你调用startActivity()方法,启动该意图过滤器时,系统将其匹配到email应用的“send”活动,并运行它
<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.
For more about creating intent filters, see the Intents and Intent Filters document. 

关于创建意图过滤器的详细信息,参考Intents and Intent Filters 文档


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文件中声明软件硬件要求,明了的指出你的应用支持的硬件类型是非常重要的,大多数声明仅仅只是信息,系统并不读取他们,但像Android市场这样的其扩展服务,将读取它们,以便让用户的设备寻找应用时,可以进行筛选.


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:
比如,如果你的应用需要有照相机,并且使用的API是2.1(API Level 7),你应在你的manifest文件中声明这些要求.这样,那些没有照相机并且Android版本低于2.1的设备,就不能从Android市场上安装你的应用.
  
<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.
但,你也可以声明你的应用使用camera,但不必须强制设备有相机。那种情况,你的应用必在运行时一个检查,以确定设备是否有一个照相机,如果没有照相机,并禁止与照相相关的功能
 
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应用的组成不仅只是代码----它还有与代码独立的资源,比如图像,音频文件,及与应用可显图像任何其他相关的资源.比如,你应该定义动画,菜单,风格,颜色,和用XML文件定义活动的布局.使用应用资源,能让你的应用在不修改任何代码的情况下容易的升级各种特性---并且通过提供一套可选取的资源--能优化你的应用在各种配置不同的设备中的表现(比如不同的语言和屏幕尺寸).


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文件中定义的其他资源中引用它.如果你的应用包括一个图片名字是logo.png(保存在res/drawable/目录 ),SDK工具将生成一个资源ID命名成R.drawable.logo,你可以用它来引用图片,并插入你的用户界面中
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的可选资源,支持许多不同的qualifiers (限定词).限定词是一个包括在你的资源目录名中的一个简短的字串,是为了定义那些资源将用在哪种配置的设备上.再如,由于设备的屏幕的方向和尺寸不同,你通常需要为你的活动定义不同的布局.比如,若设备的屏幕是竖向(高),你可能要使用一个带有垂直button 的布局,当屏幕是横向的(宽),按钮应是水平对齐的.要根据方向来改变布局,你要定义两个不同的布局,并在布局的目录名中使用相应的限定词(qualifier).然后,系统将自动根据当前的设备朝向来应用相应的布局.
翻译不当处请大家指正,联系 504747910 QQ 也欢迎更多的人加入到翻译安卓开发文档的行列中。不忘初心,方得始终。

安卓官方开发文档API GUIDE(1.1)Application Fundamentals 应用基础

标签:des   android   style   io   ar   color   os   使用   sp   

原文地址:http://blog.csdn.net/davidluo001/article/details/41496615

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!