摘要:
Page1of11Android–Part 3–XML and App LayoutsCurrent 02/27/2012OverviewIn traditional Java, GUI componentssuch as JTextFields and JButtons are arrangedandsizedby specifying a particular layout manager (such as GridLayout or BorderLayout orCardLayout) or a particular class (such as JTabbedPane).After setting a layout managerfor a Container (an Applet or a Panel, for example), individual components are added tothe layout, and the layout manager arranges and sizes themaccordingits ownrules.It is different in the Android world. First, all the GUI component classes have differentnames.Allsubclass theViewclassand so are calledviews. So Buttons, EditTexts, etc.areallknown asviews. We will call themwidgetshere.The most commonly used technique for doing widgets and layouts in Android is touse anxml file and Java code together:In an xml file:Define the screen layout, using one or more of the pre-defined layouts.Fine-tune the layout with layout attributes(properties).Declare the widgets(each is a particular type) in the order desired.Set most of thewidgetattributes(properties)(an id,text, color, font, etc.).Eclipse has a built-in GUI editor for helping with this. When you open an xml file, theeditor panel that opens has two tabs on the bottom:Graphical Layoutandxxx.xml.The .xml tab allows text editing of the xml file. TheGraphical Layoutallowsdrag-and-drop creation and editing of the layout's widgets(with a few limitations), whichautomatically creates the corresponding xml code.Right-clicking on a widget in theGraphical Layout displays a pop-up menu of attributes from which you can select and setmost attributes.Changing something in theGraphical Layoutwill immediately cause a correspondingchange in the text version. However, the new xml text will not be nicely formatted.PressingCtrl-Shift-Fwill reformat the xml (however in some cases the whole documentwill not be re-displayed correctly–in this case going to the document top and scrollingdown should correct the display inaccuracy).Changing something in the text version will also cause an immediate change in theGraphical Layoutview.Page2of11To access various widgets in your program, then, in Java code:Declarereferencestoany widgets that will be used(i.e., whose methods will becalled in the program).Obtainreferences to thesewidgets (usingFindViewById())for each, typecastingthe returned references properly, and storingthem in the declared Java referencevariables.Note that these widgets musthave the id attribute defined in the xmlfile.Usethem in the normal way (for example, get/set text in an EditText or set upevent handling for a Button).The xml Layout File and Android LayoutsNote:developer.android.com/referenceis an excellent online resource for all theproperties of all the layouts and widgets in the Android universe. Learn to use it.Each Android program–or more accurately, each Activity-derived class (since anActivity defines a screen and its functionality)-will have anxml file that defines thewidgets, their attributes, and their arrangement on the screen (the layout). This file isnormally namedmain.xmlfor an app with a single Activity, but it could be nameddifferently if you choose. Apps with multiple screens (Activities) will have one xml filefor each screen, named as you choose.Each Activitywill specify its screen–its GUI–via thesetContentView()method, usuallyinonCreate():setContentView(R.layout.main);whereRisR.java, the auto-created resource file,layoutis a sub-folder ofres("resources" in your Project's workspace) in whichthe layout xml file is stored, andmainis the name of the xml file without the .xml extension.The xml filewill containelementsand values thatdescribe one or more layouts for theActivity (multiplelayouts can bespecified sequentially, top to bottom(or right to left ifthe layout is horizontal), orlayoutscan benested).The xml file also will supplyelementsto define and describe the various widgets(withineach layout)that will populate thescreen.When Eclipse auto-generates a skeleton xml file, the firstline isstandard and will usuallynot change (xmlversion, Unicode).In th
展开