The basic functional unit of an Android application is the activity--an object of the class android.app.Activity. An activity can do many things, but by itself it does not have a presence on the screen. To give your activity a screen presence and design its UI, you work with views and viewgroups -- basic units of user interface expression on the Android platform.
Android应用程序基本的功能单元是activity:一个android.app.Activity类对象。一个activity可以做很多事情,但其本身在屏幕上并不显示。要给你的activity赋予一个屏幕表现并设计其UI,你需要views和viewgroups--Android平台基本的用户界面表示单元。
Views
A view is an object of base class android.view.View. It's a data structure whose properties store the layout and content for a specific rectangular area of the screen. A View object handles measuring and layout, drawing, focus change, scrolling, and key/gestures for the screen area it represents.
一个view是一个基于android.view.View基类的对象。是一个数据结构,其属性保存了一个屏幕上特定矩形区域的布局和内容。一个View对象为其代表的屏幕区域处理这些事情:测量和布局,画图,焦点变化,滚屏,键盘信号等。
The View class serves as a base class for widgets -- a set of fully implemented subclasses that draw interactive screen elements. Widgets handle their own measuring and drawing, so you can use them to build your UI more quickly. The list of widgets available includes Text, EditText, InputMethod, MovementMethod, Button, RadioButton, Checkbox, and ScrollView.
Viewgroups
A viewgroup is an object of class android.view.Viewgroup. As its name indicates, a viewgroup is a special type of view object whose function is to contain and manage a subordinate set of views and other viewgroups, Viewgroups let you add structure to your UI and build up complex screen elements that can be addressed as a single entity.
The Viewgroup class serves as a base class for layouts -- a set of fully implemented subclasses that provide common types of screen layout. The layouts give you a way to build a structure for a set of views.
A Tree-Structured UI
On the Android platform, you define an Activity's UI using a tree of view and viewgroup nodes, as shown in the diagram below. The tree can be as simple or complex as you need to make it, and you can build it up using Android's set of predefined widgets and layouts or custom view types that you create yourself.
![An example of a tree of views and viewgroups](http://code.google.com/android/images/viewgroup.png)
To attach the tree to the screen for rendering, your Activity calls its setContentView() method and passes a reference to the root node object. Once the Android system has the reference to the root node object, it can work directly with the node to invalidate, measure, and draw the tree. When your Activity becomes active and receives focus, the system notifies your activity and requests the root node to measure and draw the tree. The root node then requests that its child nodes draw themselves -- in turn, each viewgroup node in the tree is responsible for drawing its direct children.
As mentioned previously, each view group has the responsibility of measuring its available space, laying out its children, and calling Draw() on each child to let it render itself. The children may request a size and location in the parent, but the parent object has the final decision on where how big each child can be.
LayoutParams: How a Child Specifies Its Position and Size
Every viewgroup class uses a nested class that extends ViewGroup.LayoutParams. This subclass contains property types that define a child's size and position, in properties appropriate for that view group class.
![An example of layoutparams](http://code.google.com/android/images/layoutparams.png)
Note that every LayoutParams subclass has its own syntax for setting values. Each child element must define LayoutParams that are appropriate for its parent, although it may define different LayoutParams for its children.
All viewgroups include width and height. Many also include margins and borders. You can specify width and height exactly, though you probably won't want to do this often. More often you will tell your view to size itself either to the dimensions of its content, or to become as big as its containing object will allow.