附录 I 英文原文 Beginning Android . U sin g X M L -B a sed L a y ou ts W 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 mon approach i s to use an X M L -based la y out fi le . Dy na mic instantiation of w idg ets i s reserved for plicated scenarios, w here the w idg ets a re not know n a pile-time ( e g ., populating a colum n of radio buttons based on data retrieved off the ). W i th that in m ind, i t’s tim e to break out the X M L and learn how to la y out A ndroid activities that w a y. 1 W 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 -based 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 lem 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 ndroid’s 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 nt’s 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 low 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. 2 W 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 achi