1、构建你的第一个Android应用程序编写你的第一个Android应用程序2012-10-3 翻译:罗奕聪注意:這篇文章的一部分基於Android開放原始碼計劃(Android Open Source Project),這部分內容在知識共享 姓名標示 2.5 (CC BY 2.5) 許可下發佈此內容在知識共享 姓名標示-非商業性-相同方式分享 3.0 Unported (CC BY-NC-SA 3.0) 許可下發佈。一、创建一个Android工程An Android project contains all the files that comprise the source code for
2、your Android app. The Android SDK tools make it easy to start a new Android project with a set of default project directories and files.This lesson shows how to create a new project either using Eclipse (with the ADT plugin) or using the SDK tools from a command line.Create a Project with EclipseIn
3、Eclipse, click New Android App Projectin the toolbar.Figure 1.The New Android App Project wizard in Eclipse.1. Fill in the form that appears:o Application Nameis the app name that appears to users. For this project, use My First App.o Project Nameis the name of your project directory and the name vi
4、sible in Eclipse.o Package Nameis the package namespace for your app (following the same rules as packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. For this reason, its generally best if you use a name that begins with t
5、he reverse domain name of your organization or publisher entity. For this project, you can use something like com.example.myfirstapp. However, you cannot publish your app on Google Play using the com.example namespace.o Build SDKis the platform version against which you will compile your app. By def
6、ault, this is set to the latest version of Android available in your SDK. (It should be Android 4.1 or greater; if you dont have such a version available, you must install one using theSDK Manager). You can still build your app to support older versions, but setting the build target to the latest ve
7、rsion allows you to enable new features and optimize your app for a great user experience on the latest devices.o Minimum Required SDKis the lowest version of Android that your app supports. To support as many devices as possible, you should set this to the lowest version available that allows your
8、app to provide its core feature set. If any feature of your app is possible only on newer versions of Android and its not critical to the apps core feature set, you can enable the feature only when running on the versions that support it.Leave this set to the default value for this project.2. ClickN
9、ext.3. The following screen provides tools to help you create a launcher icon for your app.You can customize an icon in several ways and the tool generates an icon for all screen densities. Before you publish your app, you should be sure your icon meets the specifications defined in theIconographyde
10、sign guide.ClickNext.4. Now you can select an activity template from which to begin building your app.For this project, selectBlankActivityand clickNext.5. Leave all the details for the activity in their default state and clickFinish.Your Android project is now set up with some default files and you
11、re ready to begin building the app. Continue to thenext lesson.Create a Project with Command Line ToolsIf youre not using the Eclipse IDE with the ADT plugin, you can instead create your project using the SDK tools from a command line:1. Change directories into the Android SDKstools/path.2. Execute:
12、android list targetsThis prints a list of the available Android platforms that youve downloaded for your SDK. Find the platform against which you want to compile your app. Make a note of the target id. We recommend that you select the highest version possible. You can still build your app to support
13、 older versions, but setting the build target to the latest version allows you to optimize your app for the latest devices.If you dont see any targets listed, you need to install some using the Android SDK Manager tool. SeeAdding Platforms and Packages.3. Execute:android create project -target -name
14、 MyFirstApp -path /MyFirstApp -activity MainActivity -package com.example.myfirstapp Replacewith an id from the list of targets (from the previous step) and replacewith the location in which you want to save your Android projects.Your Android project is now set up with several default configurations
15、 and youre ready to begin building the app. Continue to thenext lesson.Tip:Add theplatform-tools/as well as thetools/directory to yourPATHenvironment variable.Running Your AppIf you followed theprevious lessonto create an Android project, it includes a default set of Hello World source files that al
16、low you to immediately run the app.How you run your app depends on two things: whether you have a real Android-powered device and whether youre using Eclipse. This lesson shows you how to install and run your app on a real device and on the Android emulator, and in both cases with either Eclipse or
17、the command line tools.Before you run your app, you should be aware of a few directories and files in the Android project:AndroidManifest.xmlThemanifest filedescribes the fundamental characteristics of the app and defines each of its components. Youll learn about various declarations in this file as
18、 you read more training classes.src/Directory for your apps main source files. By default, it includes anActivityclass that runs when your app is launched using the app icon.res/Contains several sub-directories forapp resources. Here are just a few:drawable-hdpi/Directory for drawable objects (such
19、as bitmaps) that are designed for high-density (hdpi) screens. Other drawable directories contain assets designed for other screen densities.layout/Directory for files that define your apps user interface.values/Directory for other various XML files that contain a collection of resources, such as st
20、ring and color definitions.When you build and run the default Android app, the defaultActivityclass starts and loads a layout file that says Hello World. The result is nothing exciting, but its important that you understand how to run your app before you start developing.Run on a Real DeviceIf you h
21、ave a real Android-powered device, heres how you can install and run your app:1. Plug in your device to your development machine with a USB cable. If youre developing on Windows, you might need to install the appropriate USB driver for your device. For help installing drivers, see theOEM USB Drivers
22、document.2. Ensure thatUSB debuggingis enabled in the device Settings (open Settings and navitage toApplications Developmenton most devices, or clickDeveloper optionson Android 4.0 and higher).To run the app from Eclipse, open one of your projects files and click Runfrom the toolbar. Eclipse install
23、s the app on your connected device and starts it.Or to run your app from a command line:1. ant debugChange directories to the root of your Android project and execute:2. adb install bin/MyFirstApp-debug.apkMake sure the Android SDKplatform-tools/directory is included in yourPATHenvironment variable,
24、 then execute:3. On your device, locateMyFirstActivityand open it.Thats how you build and run your Android app on a device! To start developing, continue to thenext lesson.Run on the EmulatorWhether youre using Eclipse or the command line, to run your app on the emulator you need to first create an
25、Android Virtual Device(AVD). An AVD is a device configuration for the Android emulator that allows you to model different devices.Figure 1.The AVD Manager showing a few virtual devices.To create an AVD:1. Launch the Android Virtual Device Manager:a. In Eclipse, click Android Virtual Device Managerfr
26、om the toolbar.b. android avdFrom the command line, change directories to/tools/and execute:2. In theAndroid Virtual Device Managerpanel, clickNew.3. Fill in the details for the AVD. Give it a name, a platform target, an SD card size, and a skin (HVGA is default).4. ClickCreate AVD.5. Select the new
27、 AVD from theAndroid Virtual Device Managerand clickStart.6. After the emulator boots up, unlock the emulator screen.To run the app from Eclipse, open one of your projects files and click Runfrom the toolbar. Eclipse installs the app on your AVD and starts it.Or to run your app from the command line
28、:1. Change directories to the root of your Android project and execute:ant debug2. Make sure the Android SDKplatform-tools/directory is included in yourPATHenvironment variable, then execute:adb install bin/MyFirstApp-debug.apk3. On the emulator, locateMyFirstActivityand open it.Thats how you build
29、and run your Android app on the emulator! To start developing, continue to thenext lesson.Building a Simple User InterfaceAlternative LayoutsDeclaring your UI layout in XML rather than runtime code is useful for several reasons, but its especially important so you can create different layouts for di
30、fferent screen sizes. For example, you can create two versions of a layout and tell the system to use one on small screens and the other on large screens. For more information, see the class aboutSupporting Different Devices.The graphical user interface for an Android app is built using a hierarchy
31、ofViewandViewGroupobjects.Viewobjects are usually UI widgets such asbuttonsortext fieldsandViewGroupobjects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.Android provides an XML vocabulary that corresponds to the subclasses ofViewandViewGroupso you can define your UI in XML using a hierarchy of UI elements.Figure 1.Illustration of howViewGroupobjects form branches in the layout and contain otherViewobjects.In this lesson, youll create a layout in XML that includes a text field and a button. In t
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1