原文地址:http://developer.android.com/guide/topics/ui/overview.html
UI Overview UI概览
All user interface elements in an Android app are built using View
and ViewGroup
objects. A View
is an object that draws something on the screen that the user can interact with. A ViewGroup
is an object that holds other View
(and ViewGroup
) objects in order to define the layout of the interface.
Android provides a collection of both View
and ViewGroup
subclasses that offer you common input controls (such as buttons and text fields) and various layout models (such as a linear or relative layout).
在一个android应用中,所有的用户接口元素(交互界面)是通过View和ViewGroup对象些实现的。一个View是一个对象,这个对象在屏幕上绘画了一些东西以至于用户可以和它交互。一个ViewGroup是一个对象,这个对象持有一些其他的View(包括ViewGroup)对象,以至于可以确定接口的布局排列(层级)。
User Interface Layout用户交互界面布局
The user interface for each component of your app is defined using a hierarchy of View
and ViewGroup
objects, as shown in figure 1. Each view group is an invisible container that organizes child views, while the child views may be input controls or other widgets that draw some part of the UI. This hierarchy tree can be as simple or complex as you need it to be (but simplicity is best for performance).
对于你的应用中每一个组件的用户交互界面,是通过View和ViewGroup对象的一个层级关系来定义的。正如图1所示。某一个viewgroup是一个不可见的容器。这个不可见的容器组织排列子views。子view可以是输入控制元件或者其他的组件。但这些子views都是绘画了一些东西的,也即可见的。这个层级树可以很简单也可以很复杂。(viewGroup里面可以包含view和viewgroup。当view里面不能包含viewgroup)
To declare your layout, you can instantiate View
objects in code and start building a tree, but the easiest and most effective way to define your layout is with an XML file. XML offers a human-readable structure for the layout, similar to HTML.
为了声明你的布局,你可以编码方式实例化view对象并且开始构建一个树。但是最简单和最有效的方式是:用xml文件定义你的布局。xml为布局提供了一种人类可读的结构,就像html一样。
The name of an XML element for a view is respective to the Android class it represents. So a <TextView>
element creates a TextView
widget in your UI, and a <LinearLayout>
element creates a LinearLayout
view group.
一个xml元素的名字对应于一个android 的类名。因此,<TextView>元素会创建一个TextView组件,在你的UI中。一个<LinearLayou>元素则会创建一个LinearLayout 类型的viewGroup。下面是一个带有一个text view和一个button的垂直布局的xml文件。
For example, a simple vertical layout with a text view and a button looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextViewandroid:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a TextView"/>
<Buttonandroid:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a Button"/>
</LinearLayout>
When you load a layout resource in your app, Android initializes each node of the layout into a runtime object you can use to define additional behaviors, query the object state, or modify the layout.
当你在你的应用中加载一个layout资源时,android会初始化layout资源中的任意一个节点把其转化成一个实时的对象。这个对象你可以用来定义额外的行为,如查询对象状态,修改布局等。
For a complete guide to creating a UI layout, see XML Layouts. 如果要看全面的创建一个UI layout的指导说明,请看XML Layouts.
User Interface Components 用户交互组件
You don't have to build all of your UI using View
and ViewGroup
objects. Android provides several app components that offer a standard UI layout for which you simply need to define the content. These UI components each have a unique set of APIs that are described in their respective documents, such as Action Bar, Dialogs, and Status Notifications.
你没有必要使用View和ViewGroup对象去构建你的所有组件。android提供了一些具有标准UI布局的应用组件,你要做的仅仅是简单的定义组件的内容。这些UI组件每一个都有一个独特的API(应用程序编程接口)集。这些内容会在相应的文档里面描述,如Action_Bar, Dialogs 和 Status_Notifications.