4.1 XML-Based Layouts
While it is possible to create and attach widgets to our activity purely through Java code, the most common way to define your layout and express the view hierarchy is with an XML layout file. XML offers a human-readable structure for the layout, much like HTML. Each element in XML is either a View or ViewGroup object (or descendant thereof). View objects are leaves in the tree, ViewGroup objects are branches in the tree.
Dynamic instantiation of widgets, for example, using addView(View) method to insert new View or ViewGroup object, is reserved for more complicated scenarios, where the widgets are not known at compile time.
As the name indicates, an XML layout is a specification of widget's relationships to each other-and to their containers-encoded in XML format. Android considers XML layouts to be resources and layout files are stored in the res/layout directory inside Android project.
Android's SDK ships with a tool (aapt) that uses the layouts. The tool should be automatically invoked by Android toolchain (Eclipse or Ant's build.xml). The aapt generates the R.java source file within the project, allowing us to access layouts and widgets within those layouts directly from Java code.
Why use XML layouts? The biggest reason, perhaps, is to assist in the creation of tools for view definition, such as a GUI builder in an IDE like Eclipse. Such GUI builders could generate Java code instead of XML. But the challenge is reading in the UI definition to support edits, which is far simpler when the data is in a structured format like XML rather than in a programming language. Moreover, keep generated definitions separated from Java code can reduce the mistakes by the programmers.
Full List of Android Tutorials
25. Activity Life Cycle
24. Media
23. HTTP Internet Connection
22. Threads
21. Content Provider
20. Notification and Service
19. Animation - Frame By Frame, Layout, and View
18. Manifest
17. Map View
16. Preferences
15. Configuring Rotation
14. On Notepad Examples
13. Intent
12. Activity Testing
11. Android JUnit Test
10. Menus
9. TabWidget, Flipper, and SlidingDrawer
8. DatePicker, TimePicker, and Clocks
7. Advanced ListView Widget
6. ListView, Spinner
5. Layouts
4. User Interface
3. Back to Hello World Again
2. Hello World
1. Introduction
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment