资源描述:
《毕业设计外文文献—android起航》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、附录I英文原文BeginningAndroid1..UsingXML-BasedLayoutsWhileitistechnicallypossibletocreateandattachwidgetstoouractivitypurelythroughJavacode,thewaywedidinChapter4,themorecommonapproachistouseanXML-basedlayoutfile.Dynamicinstantiationofwidgetsisreservedformorecompl
2、icatedscenarios,wherethewidgetsarenotknownatcompile-time(eg.,populatingacolumnofradiobuttonsbasedondataretrievedofftheInternet).Withthatinmind,it’stimetobreakouttheXMLandlearnhowtolayoutAndroidactivitiesthatway.1.11WhatIsanXML-BasedLayout?Asthenamesuggests,
3、anXML-basedlayoutisaspecificationofwidgets’relationshipstoeachother—andtotheircontainers(moreonthisinChapter7)—encodedinXMLformat.Specifically,AndroidconsidersXML-basedlayoutstoberesources,andassuchlayoutfilesarestoredintheres/layoutdirectoryinsideyourAndro
4、idproject.EachXMLfilecontainsatreeofelementsspecifyingalayoutofwidgetsandtheircontainersthatmakeuponeviewhierarchy.TheattributesoftheXMLelementsareproperties,describinghowawidgetshouldlookorhowacontainershouldbehave.Forexample,ifaButtonelementhasanattribute
5、valueofandroid:textStyle="bold",thatmeansthatthetextappearingonthefaceofthebuttonshouldberenderedinaboldfacefontstyle.Android’sSDKshipswithatool(aapt)whichusesthelayouts.ThistoolshouldbeautomaticallyinvokedbyyourAndroidtoolchain(e.g.,Eclipse,Ant’sbuild.xml)
6、.OfparticularimportancetoyouasadeveloperisthataaptgeneratestheR.javasourcefilewithinyourproject,allowingyoutoaccesslayoutsandwidgetswithinthoselayoutsdirectlyfromyourJavacode.1.22WhyUseXML-BasedLayouts?MosteverythingyoudousingXMLlayoutfilescanbeachievedthro
7、ughJavacode.Forexample,youcouldusesetTypeface()tohaveabuttonrenderitstextinbold,insteadofusingapropertyinanXMLlayout.SinceXMLlayoutsareyetanotherfileforyoutokeeptrackof,weneedgoodreasonsforusingsuchfiles.Perhapsthebiggestreasonistoassistinthecreationoftools
8、forviewdefinition,suchasaGUIbuilderinanIDElikeEclipseoradedicatedAndroidGUIdesignerlikeDroidDraw1.SuchGUIbuilderscould,inprinciple,generateJavacodeinsteadofXML.Thechallengeisre-readingtheUIdefinitionto