构建你的第一个Android应用程序Word格式.docx

上传人:b****4 文档编号:16942300 上传时间:2022-11-27 格式:DOCX 页数:11 大小:176.07KB
下载 相关 举报
构建你的第一个Android应用程序Word格式.docx_第1页
第1页 / 共11页
构建你的第一个Android应用程序Word格式.docx_第2页
第2页 / 共11页
构建你的第一个Android应用程序Word格式.docx_第3页
第3页 / 共11页
构建你的第一个Android应用程序Word格式.docx_第4页
第4页 / 共11页
构建你的第一个Android应用程序Word格式.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

构建你的第一个Android应用程序Word格式.docx

《构建你的第一个Android应用程序Word格式.docx》由会员分享,可在线阅读,更多相关《构建你的第一个Android应用程序Word格式.docx(11页珍藏版)》请在冰豆网上搜索。

构建你的第一个Android应用程序Word格式.docx

isthenameofyourprojectdirectoryandthenamevisibleinEclipse.

oPackageName 

isthepackagenamespaceforyourapp(followingthesamerulesaspackagesintheJavaprogramminglanguage).YourpackagenamemustbeuniqueacrossallpackagesinstalledontheAndroidsystem.Forthisreason,it'

sgenerallybestifyouuseanamethatbeginswiththereversedomainnameofyourorganizationorpublisherentity.Forthisproject,youcanusesomethinglike"

com.example.myfirstapp."

However,youcannotpublishyourapponGooglePlayusingthe"

com.example"

namespace.

oBuildSDK 

istheplatformversionagainstwhichyouwillcompileyourapp.Bydefault,thisissettothelatestversionofAndroidavailableinyourSDK.(ItshouldbeAndroid4.1orgreater;

ifyoudon'

thavesuchaversionavailable,youmustinstalloneusingthe 

SDKManager).Youcanstillbuildyourapptosupportolderversions,butsettingthebuildtargettothelatestversionallowsyoutoenablenewfeaturesandoptimizeyourappforagreatuserexperienceonthelatestdevices.

oMinimumRequiredSDK 

isthelowestversionofAndroidthatyourappsupports.Tosupportasmanydevicesaspossible,youshouldsetthistothelowestversionavailablethatallowsyourapptoprovideitscorefeatureset.IfanyfeatureofyourappispossibleonlyonnewerversionsofAndroidandit'

snotcriticaltotheapp'

scorefeatureset,youcanenablethefeatureonlywhenrunningontheversionsthatsupportit.

Leavethissettothedefaultvalueforthisproject.

2.Click 

Next.

3.Thefollowingscreenprovidestoolstohelpyoucreatealaunchericonforyourapp.

Youcancustomizeaniconinseveralwaysandthetoolgeneratesaniconforallscreendensities.Beforeyoupublishyourapp,youshouldbesureyouriconmeetsthespecificationsdefinedinthe 

Iconography 

designguide.

Click 

4.Nowyoucanselectanactivitytemplatefromwhichtobeginbuildingyourapp.

Forthisproject,select 

BlankActivity 

andclick 

5.Leaveallthedetailsfortheactivityintheirdefaultstateandclick 

Finish.

YourAndroidprojectisnowsetupwithsomedefaultfilesandyou’rereadytobeginbuildingtheapp.Continuetothe 

nextlesson.

CreateaProjectwithCommandLineTools

Ifyou'

renotusingtheEclipseIDEwiththeADTplugin,youcaninsteadcreateyourprojectusingtheSDKtoolsfromacommandline:

1.ChangedirectoriesintotheAndroidSDK’s 

tools/ 

path.

2.Execute:

androidlisttargets

ThisprintsalistoftheavailableAndroidplatformsthatyou’vedownloadedforyourSDK.Findtheplatformagainstwhichyouwanttocompileyourapp.Makeanoteofthetargetid.Werecommendthatyouselectthehighestversionpossible.Youcanstillbuildyourapptosupportolderversions,butsettingthebuildtargettothelatestversionallowsyoutooptimizeyourappforthelatestdevices.

Ifyoudon'

tseeanytargetslisted,youneedtoinstallsomeusingtheAndroidSDKManagertool.See 

AddingPlatformsandPackages.

3.Execute:

androidcreateproject--target<

target-id>

--nameMyFirstApp\--path<

path-to-workspace>

/MyFirstApp--activityMainActivity\--packagecom.example.myfirstapp

Replace 

<

withanidfromthelistoftargets(fromthepreviousstep)andreplace 

withthelocationinwhichyouwanttosaveyourAndroidprojects.

YourAndroidprojectisnowsetupwithseveraldefaultconfigurationsandyou’rereadytobeginbuildingtheapp.Continuetothe 

Tip:

Addthe 

platform-tools/ 

aswellasthe 

directorytoyour 

PATH 

environmentvariable.

RunningYourApp

Ifyoufollowedthe 

previouslesson 

tocreateanAndroidproject,itincludesadefaultsetof"

HelloWorld"

sourcefilesthatallowyoutoimmediatelyruntheapp.

Howyourunyourappdependsontwothings:

whetheryouhavearealAndroid-powereddeviceandwhetheryou’reusingEclipse.ThislessonshowsyouhowtoinstallandrunyourapponarealdeviceandontheAndroidemulator,andinbothcaseswitheitherEclipseorthecommandlinetools.

Beforeyourunyourapp,youshouldbeawareofafewdirectoriesandfilesintheAndroidproject:

AndroidManifest.xml

The 

manifestfile 

describesthefundamentalcharacteristicsoftheappanddefineseachofitscomponents.You'

lllearnaboutvariousdeclarationsinthisfileasyoureadmoretrainingclasses.

src/

Directoryforyourapp'

smainsourcefiles.Bydefault,itincludesan 

Activity 

classthatrunswhenyourappislaunchedusingtheappicon.

res/

Containsseveralsub-directoriesfor 

appresources.Herearejustafew:

drawable-hdpi/

Directoryfordrawableobjects(suchasbitmaps)thataredesignedforhigh-density(hdpi)screens.Otherdrawabledirectoriescontainassetsdesignedforotherscreendensities.

layout/

Directoryforfilesthatdefineyourapp'

suserinterface.

values/

DirectoryforothervariousXMLfilesthatcontainacollectionofresources,suchasstringandcolordefinitions.

WhenyoubuildandrunthedefaultAndroidapp,thedefault 

classstartsandloadsalayoutfilethatsays"

HelloWorld."

Theresultisnothingexciting,butit'

simportantthatyouunderstandhowtorunyourappbeforeyoustartdeveloping.

RunonaRealDevice

IfyouhavearealAndroid-powereddevice,here'

showyoucaninstallandrunyourapp:

1.PluginyourdevicetoyourdevelopmentmachinewithaUSBcable.Ifyou’redevelopingonWindows,youmightneedtoinstalltheappropriateUSBdriverforyourdevice.Forhelpinstallingdrivers,seethe 

OEMUSBDrivers 

document.

2.Ensurethat 

USBdebugging 

isenabledinthedeviceSettings(openSettingsandnavitageto 

Applications>

Development 

onmostdevices,orclick 

Developeroptions 

onAndroid4.0andhigher).

ToruntheappfromEclipse,openoneofyourproject'

sfilesandclickRun 

fromthetoolbar.Eclipseinstallstheapponyourconnecteddeviceandstartsit.

Ortorunyourappfromacommandline:

1.

antdebug

ChangedirectoriestotherootofyourAndroidprojectandexecute:

2.

adbinstallbin/MyFirstApp-debug.apk

MakesuretheAndroidSDK 

directoryisincludedinyour 

environmentvariable,thenexecute:

3.Onyourdevice,locate 

MyFirstActivity 

andopenit.

That'

showyoubuildandrunyourAndroidapponadevice!

Tostartdeveloping,continuetothe 

RunontheEmulator

Whetheryou’reusingEclipseorthecommandline,torunyourappontheemulatoryouneedtofirstcreateanAndroidVirtualDevice 

(AVD).AnAVDisadeviceconfigurationfortheAndroidemulatorthatallowsyoutomodeldifferentdevices.

TheAVDManagershowingafewvirtualdevices.

TocreateanAVD:

1.LaunchtheAndroidVirtualDeviceManager:

a.InEclipse,clickAndroidVirtualDeviceManager 

fromthetoolbar.

b.

androidavd

Fromthecommandline,changedirectoriesto<

sdk>

/tools/ 

andexecute:

2.Inthe 

AndroidVirtualDeviceManager 

panel,click 

New.

3.FillinthedetailsfortheAVD.Giveitaname,aplatformtarget,anSDcardsize,andaskin(HVGAisdefault).

4.Click 

CreateAVD.

5.SelectthenewAVDfromthe 

Start.

6.Aftertheemulatorbootsup,unlocktheemulatorscreen.

fromthetoolbar.EclipseinstallstheapponyourAVDandstartsit.

Ortorunyourappfromthecommandline:

1.ChangedirectoriestotherootofyourAndroidprojectandexecute:

2.MakesuretheAndroidSDK 

3.Ontheemulator,locate 

showyoubuildandrunyourAndroidappontheemulator!

BuildingaSimpleUserInterface

AlternativeLayouts

DeclaringyourUIlayoutinXMLratherthanruntimecodeisusefulforseveralreasons,butit'

sespeciallyimportantsoyoucancreatedifferentlayoutsfordifferentscreensizes.Forexample,youcancreatetwoversionsofalayoutandtellthesystemtouseoneon"

small"

screensandtheotheron"

large"

screens.Formoreinformation,seetheclassabout 

SupportingDifferentDevices.

ThegraphicaluserinterfaceforanAndroidappisbuiltusingahierarchyof 

View 

and 

ViewGroup 

objects. 

objectsareusuallyUIwidgetssuchas 

buttons 

or 

textfields 

andViewGroup 

objectsareinvisibleviewcontainersthatdefinehowthechildviewsarelaidout,suchasinagridoraverticallist.

AndroidprovidesanXMLvocabularythatcorrespondstothesubclassesof 

soyoucandefineyourUIinXMLusingahierarchyofUIelements.

Illustrationofhow 

objectsformbranchesinthelayoutandcontainother 

objects.

Inthislesson,you'

llcreatealayoutinXMLthatincludesatextfieldandabutton.Int

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 从业资格考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1