登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 物联网工程 > 正文

基于智能手机的个人小秘书外文翻译资料

 2022-11-26 08:11  

Beginning Android

Mark Murphy

1. Using XML-Based Layouts

While 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, itrsquo;s time to break out the XML and learn how to lay out Android activities that way.

1.1What Is an XML-Based Layout?

As the name suggests, an XML-based layout is a specification of widgetsrsquo; 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 thetext appearing on the face of the button should be rendered in a boldface font style.

Androidrsquo;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, Antrsquo;s build.xml). Ofparticular 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.

1.2Why 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 somebodyrsquo;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. Microsoftrsquo;s XAML2, Adobersquo;s Flex3, and Mozillarsquo;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.

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.

lt;?xml version='1.0' encoding='utf-8'?gt;

lt;Button xmlns:android='http://schemas.android.com/apk/res/android'

android:id='@ id/button'

android:text=''

android:layout_width='fill_parent'

android:layout_height='fill_parent'/gt;

The class name of the widget—Button—forms the name of the XML element. Since Button is an Android-supplied widget, we can just use the bare class name. If you create your own widgets as subclasses of android.view.View, you would need to provide a full package declaration as well.

The root element needs to declare the Android XML namespace:

xmlns:android='http://schemas.android.com/apk/res/android'

All other elements will be children of the root and will inherit that namespace declaration.

Because we wantto reference this button from our Java code, we need to give it an identifier via the android:id attribute. We will cover this concept in greater detail later in this chapter.

The remaining attributes are properties of this Button instance:

bull;android:text indicates the initial text to be displayed on the button face (in this case, an empty string)

bull;android:layout_width and android:layout_height tell Android to have the buttonrsquo;s

width and height fill the “parent”, in this case the entire screen—these attributes will be covered in greater detail in Chapter 7.

Since this single widget is the only content in our activity, we only need this single element. Complex UIs will require a whole tree of elements, representing the widgets

and containers that control their positioning. All the remaining chapters of this book

will use the XML layout form whenever practical, so there are dozens of other examples of more complex layouts for you to peruse from Chapter 7 onward.

1.3Whatrsquo;s with the @ Signs?

Many widgets and containers only need to appear in the XML layout file and do not need to be referenced in your Java code. For example, a static label (TextView) frequently only needs to be in the layout file to indicate where it should appear. These sorts of elements in the XML file do not need to have the android:id attribu

剩余内容已隐藏,支付完成后下载完整资料


Android起航

1 使用XML进行布局

虽然纯粹通过Java代码在activity上创建和添加部件,在技术上是可行的,我们在第4章中做的一样,更常见的方法是使用一种基于XML的布局文件。动态的小部件实例保留更多,情况复杂,小工具在编译时不为人所知。例如,在数据检索了互联网基础上将单选按钮填充柱。

考虑到这一点,现在是时候来学习如何用此种方式来布置Android activities。

1.1 什么是基于XML的布局?

正如其名称所示,一个基于XML的布局是一个关系到每个规格的小部件,和他们的容器(更多关于此内容的在第7章)编码的XML格式。具体来说,Android认为基于XML的布局是资源,因此布局文件存储在res /在你的Android项目布局目录中。

每个XML文件包含一个指定的部件和容器布局元素树,一种意见认为构成层次。对XML元素的属性,描述一个部件应如何看或者一个容器应如何运转。例如,如果一个按钮元素。

有一个Android的属性值:文字样式=“bold”,这意味着该文本出现在按钮的表面应该是呈现一个粗体字体样式.

Android的SDK中附带一个使用的布局的工具(aapt)。这个工具应自动调用你的Android工具链(例如,Eclipse中,Antrsquo;s build.xml)。作为一个开发人员,尤其重要的是,在您的项目中aapt生成R.java源文件,让您能在那些布局中直接从Java代码中获取布局和部件。

1.2为什么基于XML的布局?

使用XML布局文件做的大部分都可以通过Java代码。例如,你可以使用setTypeface()命令一个按钮使用粗体文本,而不是在一个XML布局中使用属性。由于XML布局是为你跟踪的另一个文件,所以我们需要好的理由来使用这样的文件。

也许最大的原因是为了在视图定义中协助工具的建立,如IDE中一个GUI创建者像Eclipse或者一个像DroidDraw1设计GUI图形用户界面建设者。这样GUI建设者们,在原则上,生成Java代码而不是XML。目前的挑战是重新阅读用户界面的定义,以支持编辑,也就是说,如果是像XLM的结构公式数据比一个程序语言中的数据简单的多。此外,保持生成的XML定义从手写的Java代码中分离,使得某人定制的来源意外重新生成不太可能。

XML形成一个良好的中间立场,使工具作家使用更简便,程序员需要时手工工作更简易。

此外,XML作为一个GUI定义格式是越来越普遍。微软的XAML,Adobe的Flex,和Mozilla的XUL都采取Android类似的方法:把布局细节放在一个XML文件和把编程智慧资料放在源文件(例如,XUL中的JavaScript)。许多不太知名的图形用户界面框架,如ZK,还使用视图定义的XML。而“随大流”并不一定是最好的政策,但他们有优势帮助从任何其他XML为中心的观点描述语言轻松进入Android。

好了,那么基于XML的布局是什么样子的?下面是以前的章节的示例应用程序按钮,转换成一个XML布局文件:

lt;?xml version='1.0' encoding='utf-8'?gt;

lt;Button xmlns:android='http://schemas.android.com/apk/res/android'

android:id='@ id/button'

android:text=''

android:layout_width='fill_parent'

android:layout_height='fill_parent'/gt;

部件,按钮的类名称形成XML元素的名称。因为按钮是Android提供的部件,我们可以只使用裸类的名称。如果您创建自己的部件作为android.view.View子小部件,您也将需要提供一个完整的包声明(如com.commonsware.android.MyWidget)。

因为我们要引用这个来自Java代码的按钮,我们需要通过android给它一个标识符:id属性。我们将在本章后面更详细的介绍这个概念。

其余的属性是此按钮实例属性:

bull;android:文字表示的初始文本将显示在按钮(这种情况显示空字符串)

bull;android:layout_width和Android:layout_height命令android有按钮的

宽度和高度填写“parent”,这种情况下,整个屏幕。将这些属性将在第7章中详解。由于这个单一部件是activity的仅有内容,我们只需要这一个因素。复杂的用户界面将需要整个树的元素,代表工具和容器,控制自己的定位。所有的这本书余下的章节将使用XML布局,所以还有数十种更复杂的其他布局实例,请前进到第七章仔细阅读。

1.3@符号有什么用途?

许多部件和容器只需要出现在XML布局文件,不须引用在Java代码。例如,一个静态标签(TextView)只需要在布局文件中以表明它应该出现在那里。在XML文件中各种元素文件不需要有android:id属性给他们一个名称。任何你想要在Java资源中使用的东西,都需要一个android:id。

该公约是使用@ id...作为ID值,其中的...代表你locallyunique名称有问题的部件。在上一节的XML布局的例子中,@ id是按钮控件的标识符。

android提供了一些特殊的android:ID值,形式@android:id/...我们将在这本书的不同章节中看到这些,例如第八章和第十章。

我们将这些附加到Javahellip;如何?

既然你有意建立一个XML配置文件的工具和容器,名为main.xml存储res/layout,所有你需要的是一个在您activity的OnCreate()回调以使用该版式:setContentView(R.layout.main);

这是相同的setContentView(),我们前面使用,通过它的一个视图子类的实例(在这种情况下,一个按钮)。该android制造的观点,来自我们的布局,是从访问该代码生成的R类。所有的布局都可以访问R.layout,由基地键控布局文件的名称-main.xml result in R.layout.main.

要访问确定部件,使用findViewById(),在数字标识符传递有问题的部件。这一数字标识符生成的R类由android在R.id.something(其中一些是你正在寻找的具体部件)。这些部件是只是子类的视图,就像我们在第四章中创建Button实例。

剩下的部分在原始的Now演示中,按钮的表面便会显示当前的时间,这将反映当按钮被最后按下时显示的时间(或者如果在按钮尚未被按下时显示)。这种逻辑仍然适用,即使在该修订演示(NowRedux)中。尽管如此,在activityrsquo;s onCreate() callback中的实例,我

们可以从XML的布局参考一个例子:

package com.commonsware.android.layouts;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import java.util.Date;

public class NowRedux extends Activity

implements View.OnClickListener {

Button btn;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

btn=(Button)findViewById(R.id.button);

btn.setOnClickListener(this);

updateTime();

}

public void onClick(View view) {

updateTime();

}

private void updateTime() {

btn.setText(new Date().toString());}

}

第一个区别是,在Java代码中设置内容视图作为视图,我们将它设置为引用的XML布局(setContentView(R.layout.main))。该R.java源文件将被更新,当我们重建这个项目,包括对我们布局参考文件(存储在main.xml in our projectrsquo;s res/layout directory )。

另一个不同是,我们需要亲手实验按钮实例,我们使用findViewById()调用。既然我们发现按钮为@ id/button,我们可以参考按钮的标识符R.id.button。现在,随着手手头上的实例,我们可以设置回调并根据需要设置标签。

2 使用基本组件

每一个GUI工具包都有一些基本的部件:字段,标签,按钮等,Android的工具包在范围内没有不同,其基本部件将提供一个良好的介绍,关于这些部件在Android activities中是如何运行的。

指派标签是最简单的部件是标签,在Android提到的作为一个TextView。像大多数的GUI工具包,标签的文本是不可被用户直接编辑的。通常情况下,它们被用来确定相邻部件(例如,一个“姓名:”一个填充姓名前的标签)。

在Java中,你可以通过创建一个TextView的实例l来创建一个标签。更常见的,虽然,

你将通过添加一个TextView元素到布局来在XML布局文件中创建标签,与一个Android:文本属性来设置标签的本身价值。如果您需要交换基于某些标准的标签,例如国际化,你可能想使用XML中的资源参考代替,这些将在第9章叙述。例如,在Basic/Label项目中,你将找到下列布局文件:

lt;?xml version='1.0' encoding='utf-8'?gt;

lt;TextView xmlns:android='http://schemas.android.com/apk/res/android'

android:layout_width='fill_parent'

android:layout_height='wrap_content'

android:text='You were expecting something profound?'

只是单独的布局,由android的项目生成器提供的Java源的(如activityCreator),生成应用程序。

2.1按钮,归属于谁?

我们已经在第4和第5章看到了按钮部件用法。按钮是文本视图的一个子类,所以一切都在上一节讨论了,按钮格式所面临的问题仍然成立。

Android有两个部件,来帮助你将照片嵌入activities:ImageView和ImageButton。正如名称所暗示的,他们是分别对于文本视图和按钮基于图像的类似物。每个部件带有一个android:src属性(在一个XML布局中),指明使用什么图片。这些通常引用一个可绘制的资源,在讲资源的这个章节中更详细地描述了。您还可以通过setImageURI()从内容提供商在Uri基础上设置图像。

ImageButton控件,一个ImageView子类,混合在标准按钮行为中,应对点击和诸如此类的东西。例如,从Basic/ImageView样本项目中看main.xml布局,这可以在http://apress.com以及所有其他代码示例种找到。

lt;?xml version='1.0' encoding='utf-8'?gt;

lt;ImageView xmlns:android='http://schemas.android.com/apk/res/android'

android:id='@ id/icon'

android:layout_width='fill_parent'

android:layout_height='fill_parent'

android:adjustViewBounds='true'

android:src='@drawable/molecule'

/gt;

2.2绿色字段或者其他色彩

紧接着按钮和标签,字段是大多数GUI工具包的第三个“锚”。在Android中,他们通过EditText部件运行,它是标签的一个子类TextView。

随着标准TextView属性(例如,Android:文本样式),EditText有许多其他方面可以帮助你创建字段,包括:除了这些,你可以使用专门配置字段输入方法,如android:仅数字输入numeric,android:为笼罩密码输入密码,还有Android:phoneNumber进入电话号码。如果你想创建自己的输入法计划(如邮政编码,社会安全号码),您需要创建自己的执行情况InputMethod接口,然后通过android设定字段来使用:inputMethod。例如,从the Basic/Field项目,这里是一个XML布局文件显示EditText:

lt;?x

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[26192],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图