android xml app,Android Part 3 XML and App Layouts

摘要:

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

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值