毕业设计外文文献—Android起航.docx
《毕业设计外文文献—Android起航.docx》由会员分享,可在线阅读,更多相关《毕业设计外文文献—Android起航.docx(11页珍藏版)》请在沃文网上搜索。
1、附录 I 英文原文Beginning Android1. .U sin g X M L -B a sed L a y ou tsW hile i t i s technically possible to create and a ttach w idg ets to our activity purely throug h J ava code, the w ay w e did in C hapter 4 , the more com mon approach i s to use an X M L -based la y out fi le . Dy na mic instantiati
2、on of w idg ets i s reserved for more complicated scenarios, w here the w idg ets a re not know n a t com pile-time ( e g ., populating a colum n of radio buttons based on data retrieved off the Internet).W i th that in m ind, i ts tim e to break out the X M L and learn how to la y out A ndroid acti
3、vities that w a y.1.1 1W h a t Is a n X M L -B a sed L a y ou t?A s the name sug g ests, an X M L -based la y out is a specification of w idg ets re la tionships to each other and to their containers ( m ore on this in C hapter 7 ) encoded in X M L format. S pecifically, A ndroid considers X M L -ba
4、sed la y outs to be resources, and as such lay out fi les are stored in the res /la y out directory inside y our A ndroid project.Each X M L fi le contains a tree of e lem ents specify ing a lay out of w idg ets and their containers that make up one v iew hierarchy. The a ttributes of the X M L e le
5、m ents a re properties, describing how a w idg et should look or how a container should behave. For ex ample, if a B utton element has an a ttribute value of android: tex tS ty le = bold, that means that the text appearing on the face of the button should be rendered in a boldface font sty le.A ndro
6、ids S DK ships w ith a tool ( aapt) w hich uses the lay outs. This tool should be automatically invoked by y our A ndroid tool chain ( e .g ., Eclipse, A nts build.x ml). Of particular im portance to y ou as a developer i s that aapt g enerates the R .java source fi le w i thin y our project, a l lo
7、w ing y ou to access lay outs and w idg ets w i thin those l a y outs directly from y our J ava code.1.2 2W h y U se X M L -B a sed L a y ou ts?M ost every thing y ou do using X M L la y out fi les can be achieved throug h J ava code. For ex ample, y ou could use setTy peface() to have a button rend
8、er i ts tex t in bold, instead of using a property in an X M L lay out. S ince X M L la y outs are y et another fi le for y ou to keep track of, w e need g ood reasons for using such files.Perhaps the big g est reason i s to assist in the creation of tools for view definition, such as a GU I builder
9、 in an IDE like Eclipse or a dedicated A ndroid GU I desig ner like DroidDraw 1 . S uch GU I builders could, in principle, g enerate J ava code instead of X M L. The challeng e i s re-reading the U I definition to support edits that i s far s im pler if the data is in a structured format l ike X M L
10、 than in a prog ram ming lang uag e . M oreover, keeping g enerated X M L definitions separated from hand-w ri tten J ava code makes i t less l ikely that som ebody s custom -c ra fted source w i l l g et clobbered by accident w hen the g enerated bits g et re-g enerated. X M L form s a nice m iddle
11、 g round betw een som ething that i s easy for tool-w riters to use and easy for prog ramm ers to w ork w i th by hand as needed.A l so, X M L as a GU I definition form a t i s becom ing more com monplace. M icrosofts X A M L 2 , A dobe s Flex 3 , and M ozilla s X U L 4 a l l take a sim i la r appro
12、ach to that of A ndroid: put la y out details in an X M L fi le and put prog ramm ing smarts in source files ( e .g ., J ava S cript for X U L).M any less-w el l -know n GU I framew orks, such as ZK5 , a l so use X M L for view definition. W hile “fol low ing the herd”is not necessarily the best pol
13、icy, i t does have the advantag e of helping to ease the transition into A ndroid from any other X M L -centered view description lang uag e .la y out fi le , found in the La y outs /Now R edux sam ple project. This code sample a long w i th a ll others in this chapter can be found in the S ource C
14、ode a rea of http:/ .The c la ss name of the w idget B utton forms the name of the X M L element. S ince B utton is an A ndroid-supplied w idg et, w e can just use the bare class name. If y ou create y our ow n w idg ets as subclasses of android.view.View, y ou w ould need to provide a full packag e
15、 declaration as w ell.The root e lement needs to declare the A ndroid X M L nam espace: x mlns: android=http:/ schem /a pk /re s /a ndroidA l l other elements w i l l be children of the root and w i l l inherit that nam espace declaration.B ecause w e w ant to reference this button from our J ava co
16、de, w e need to g ive i t an identifier via the android: id a ttribute. W e w i l l cover this concept in g reater detail la ter in this chapter.The remaining a ttributes a re properties of this B utton instance: android: text indicates the initial tex t to be display ed on the button face ( in this
17、 case, an empty string ) android: lay out_ w idth and android: la y out_ heig ht te l l A ndroid to have the buttons w idth and heig ht fi l l the “pa rent”, in this case the entire screen these a ttributes w i l l be covered in g reater detail in C hapter 7 .S ince this sing le w idg et i s the onl
18、y content in our activity, w e only need this sing le e lement. C omplex U Is w i l l require a w hole tree of elements, representing the w idg ets and containers that control their positioning . A ll the remaining chapters of this book w i l l use the X M L lay out form w henever practical, so ther
19、e are dozens of other examples of more complex la y outs for y ou to peruse from C hapter 7 onw ard.1.3 3 W h a t s w ith th e S ign s?M any w idg ets and containers only need to appear in the X M L la y out fi le and do not need to be eferenced in y our J ava code. For example, a s ta tic label ( T
20、ex tView ) frequently only needs to be in the la y out fi le to indicate w here it should appear. These sorts of elem ents in the X M L file do not need to have the android: id attribute to g ive them a nam e.A ny thing y ou do w ant to use in y our J ava source, thoug h, needs an android: id.The co
21、nvention is to use +id/. as the id value, w here the . represents y our locally unique name for the w idg et in question. In the X M L lay out ex ample in the preceding section, +id/bu tton i s the identifier for the B utton w idg et.A ndroid provides a few special android: id values, of the form an
22、droid: id/. W e w i l l see some of these in various chapters of this book, such as C hapters 8 and 10 .W e A tta ch These to the J ava How ?Given that y ou have painstaking l y set up the w idg ets and containers in an X M L la y out filenamed main.x m l stored in res /la y out, a l l y ou need is
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
20 积分
下载 | 加入VIP,下载更划算! |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 毕业设计 外文 文献 Android 起航