AnAndroid basic components use

原创 2012年04月05日 09:04:45
Android basic components use1.1Using XML-Based LayoutsWhile it is technically possible to create and attach widgets to our activity purely through Java code, the way we did in Chapter 4, the more common approach is to use an XML-based layout file. Dynamic instantiation of widgets is reserved for more complicated scenarios, where the widgets are not known at compile-time (e g., populating a column of radio buttons based on data retrieved off the Internet). With that in mind, it’s time to break out the XML and learn how to lay out Android activities that way. What Is an XML-Based Layout? As the name suggests, an XML-based layout is a specification of widgets’ relationships to each other—and to their containers (more on this in Chapter 7)—encoded in XML format. Specifically, Android considers XML-based layouts to be resources, and as such layout files are stored in the res/layout directory inside your Android project. Each XML file contains a tree of elements specifying a layout of widgets and their containers that make up one view hierarchy. The attributes of the XML elements are properties, describing how a widget should look or how a container should behave. For example, if a Button element has an attribute value of android:textStyle = "bold", that means that the text appearing on the face of the button should be rendered in a boldface font style. Android’s SDK ships with a tool (aapt) which uses the layouts. This tool should be automatically invoked by your Android tool chain (e.g., Eclipse, Ant’s build.xml). Of particular importance to you as a developer is that aapt generates the source file within your project, allowing you to access layouts and widgets within those layouts directly from your Java code. Why Use XML-Based Layouts? Most everything you do using XML layout files can be achieved through Java code. For example, you could use setTypeface() to have a button render its text in bold, instead of using a property in an XML layout. Since XML layouts are yet another file for you to keep track of, we need good reasons for using such files. Perhaps the biggest reason is to assist in the creation of tools for view definition, such as a GUI builder in an IDE like Eclipse or a dedicated Android GUI designer like DroidDraw1. Such GUI builders could, in principle, generate Java code instead of XML. The challenge is re-reading the UI definition to support edits—that is far simpler if the data is in a structured format like XML than in a programming language. Moreover, keeping generated XML definitions separated from hand-written Java code makes it less likely that somebody’s custom-crafted source will get clobbered by accident when the generated bits get re-generated. XML forms a nice middle ground between something that is easy for tool-writers to use and easy for programmers to work with by hand as needed. Also, XML as a GUI definition format is becoming more commonplace. Microsoft’s XAML2, Adobe’s Flex3, and Mozilla’s XUL4 all take a similar approach to that of Android: put layout details in an XML file and put programming smarts in source files (e.g., JavaScript for XUL). Many less-well-known GUI frameworks, such as ZK5, also use XML for view definition. While “following the herd” is not necessarily the best policy, it does have the advantage of helping to ease the transition into Android from any other XML-centered view description language. OK, So What Does It Look Like? Here is the Button from the previous chapter’s sample application, converted into an XML layout file, found in the Layouts/NowRedux sample project. This code sample along with all others in this chapter can be found in the Source Code area of


FW:Use the OnReadyStateChange Property in Visual Basic and Visual C/C++

Link: The onreadystatecha...

R basic use —用R管理目录文件 (转自conan大神,纯手打,帮助记忆,可作为索引)


深入理解openstack网络架构(2)----Basic Use Cases

在上一篇文章中,我们了解了几个网络组件,如openvswitch/network namespace/Linux bridges/veth pairs。这篇文章中,我们将用3个简单的use case,...

basic use of pspice via ORCAD caputre

  • 2011年07月30日 13:02
  • 155KB
  • 下载

How to use an ASP.NET application to query an Indexing Service catalog by using Visual Basic .NET

This step-by-step article describes how to create an Indexing Service catalog. It also describes how...
  • jamex
  • jamex
  • 2011年07月21日 16:43
  • 715

AnAndroid 定时器方法 。

在android中,经常用到的定时器主要有以下几种实现: 一、采用Handler与线程的sleep(long )方法 二、采用Handler的postDelayed(Runnable, long)...
您举报文章:AnAndroid basic components use