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 R.java 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 http://apress.com.

ionic 本地存在pouchDB+sqlite 浏览器运行正常,手机运行异常的处理方法

Microsoft Windows [版本 10.0.10586] (c) 2015 Microsoft Corporation。保留所有权利。 C:\Users\admin>e: E:...
  • lanbooli
  • lanbooli
  • 2016年12月21日 09:11
  • 950

【持续集成CI】mac系统下安装jenkins环境

【1】环境准备 java 1.8+ 下载链接http://www.cr173.com/mac/122803.html#down-link 【2】安装jenkins 参考链接http://www.jia...
  • mashang123456789
  • mashang123456789
  • 2017年11月10日 15:10
  • 779

HTTP使用BASIC认证的原理及实现方法

一.   BASIC认证概述 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份证的方法,当一个客户端向HTTP服务器进行数据请求时,如...
  • lvxinzhi
  • lvxinzhi
  • 2015年10月09日 14:46
  • 1918

AndroidStudio1.5 安装完成后初次启动卡在download Components解决办法

我的安装条件是:window7 jdk8 SDK已经有了情况下,我把安装时候安装SDK给去掉了。同时选择了已存在的SDK地址情况下。我不知道我在这样情况下为什么在初次启动时候还要下载一些Build包...
  • JQ_AK47
  • JQ_AK47
  • 2016年11月22日 23:49
  • 967

Android Studio 安装完成,启动时一直download Components解决办法

安装完成之后第一次启动的时候会首先显示Fetching Android SDK component information,等一会儿在Setup Wizard - Downloading Compon...
  • xiaoqiang_0719
  • xiaoqiang_0719
  • 2016年05月27日 16:51
  • 3672

认证模式之Basic模式

HTTP协议规范中有两种认证方式,一种是Basic认证,另外一种是Digest认证,这两种方式都属于无状态认证方式,所谓无状态即服务端都不会在会话中记录相关信息,客户端每次访问都需要将用户名和密码放置...
  • wangyangzhizhou
  • wangyangzhizhou
  • 2016年04月15日 18:09
  • 8615

Apache认证配置之一Basic认证(一)

经常上网的读者会遇到这种情况:访问一些网站的某些资源时,浏览器弹出一个对话框,要求输入用户名和密码来获取对资源的访问。...
  • yupei881027
  • yupei881027
  • 2014年05月29日 18:31
  • 1340

Basic表示一个简单的属性

5、@Basic(fetch=FetchType,optional=true)   可选   @Basic表示一个简单的属性到数据库表的字段的映射,对于没有任何标注的getXxxx()方法,默认 ...
  • u014131893
  • u014131893
  • 2014年03月25日 23:19
  • 1998

Http认证之Basic认证

文章主要讲如何在tomcat中配置Basic认证以及工作流程: Tomcat配置: 1 在tomcat的webapps下新建一个目录authen,再建立子目录subdir,下面放一个ind...
  • zmx729618
  • zmx729618
  • 2016年05月11日 10:04
  • 2192

Visual Basic快捷教程——流程控制

在顺序执行的过程中,有时希望计算机根据条件判断来选择性地执行(或者不执行)一些代码,这时就需要用到选择结构(或称分支结构)。另外一些时候,希望计算机能够(有条件地)重复地执行某一段代码,这时就需要用到...
  • baimafujinji
  • baimafujinji
  • 2017年04月04日 15:14
  • 3091
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:AnAndroid basic components use
举报原因:
原因补充:

(最多只允许输入30个字)