User Interface用户界面

In an Android application, the user interface is built using View and ViewGroup objects. There are many types of views and view groups, each of which is a descendant of the View class.
在Android应用中,用户界面是用 View和 ViewGroup对象构建的。有许多的views和view组类型,它们都是View类的后代。
View objects are the basic units of user interface expression on the Android platform. The View class serves as the base for subclasses called "widgets," which offer fully implemented UI objects, like text fields and buttons. The ViewGroup class serves as the base for subclasses called "layouts," which offer different kinds of layout architecture, like linear, tabular and relative.
View对象是Android平台上用户界面表达方式的最基本单元。View类服务于一个叫做”widgets”的子类,它提供了所有的UI对象的实现,像文本域,及按钮。ViewGroup类服务于叫做”layouts”的子类,它提供了不同的布局架构,像线性,表格及相对布局。
A View object is a data structure whose properties store the layout parameters and content for a specific rectangular area of the screen. A View object handles its own measurement, layout, drawing, focus change, scrolling, and key/gesture interactions for the rectangular area of the screen in which it resides. As an object in the user interface, a View is also a point of interaction for the user and the receiver of the interaction events.
一个View对象是一个数据结构,它的属性存储了屏上某块矩形区域的布局参数和内容。一个View对象处理它自己的所在屏幕区域内的尺寸,布局,绘制,焦点改变,滚动及按键/手势交互。作为一个用户界面的对象,一个View也是一个用户效互的位置,并且接收交互事件。
View Hierarchy 视图的层次
On the Android platform, you define an Activity's UI using a hierarchy of View and ViewGroup nodes, as shown in the diagram below. This hierarchy tree can be as simple or complex as you need it to be, and you can build it up using Android's set of predefined widgets and layouts, or with custom Views that you create yourself.
在android平台上,你可以用View和ViewGroup节点的层次定义活动的UI,显示如下图。这个层次树可以根据你的需要而变得简单或者复杂,并且你也能使用Android一套预定义的widgets和布局来构建,或者用你自己创建的处定义Views来构建。

In order to attach the view hierarchy tree to the screen for rendering, your Activity must call the setContentView() method and pass a reference to the root node object. 为了以图示的层次树渲染屏幕,你的活动必须调用setContentView() 方法,并传递一个根节点对象的引用给它。
The Android system receives this reference and uses it to invalidate, measure, and draw the tree. Androidt系统收到这个引用并且用它支无效,测量和绘制这颗树。
The root node of the hierarchy requests that its child nodes draw themselves — in turn, each view group node is responsible for calling upon each of its own child views to draw themselves. 层次的根结点请求它的孩子轮流绘制它自己,每个视图组负责调用每个基于它的子视图绘制它们自己。
The children may request a size and location within the parent, but the parent object has the final decision on where how big each child can be. 子节点可能从父节点请求一个大小和位置,但是父节点最终决定每个孩子可能有多大。
Android parses the elements of your layout in-order (from the top of the hierarchy tree), instantiating the Views and adding them to their parent(s).Android按顺序分析你的布局的元素(从层次树顶),实例化Views并把它们加入到它们的父亲上。
Because these are drawn in-order, if there are elements that overlap positions, the last one to be drawn will lie on top of others previously drawn to that space.因为它们是按顺序绘制的,如果有一些元素复盖了位置,最后的那个将绘制在其他的先前绘制的上面。
For a more detailed discussion on how view hierarchies are measured and drawn, read How Android Draws Views.关于视图层的测量和绘制的详细讨论,阅读How Android Draws Views.

Layout布局
The most common way to define your layout and express the view hierarchy is with an XML layout file.最常用的定义布局和表示视图层的方法是使用XML布局文件。 
XML offers a human-readable structure for the layout, much like HTML. Each element in XML is either a View or ViewGroup object (or descendant thereof). XML为布局提供了一种可阅读的结构,很像HTML。XML中每个元素要么是View要么是ViewGroup对象(或者他们的后代)
View objects are leaves in the tree, ViewGroup objects are branches in the tree (see the View Hierarchy figure above).View对象是树上的叶子,而ViewGroup是树的分支(参考上面的树的层次图)。
The name of an XML element is respective to the Java class that it represents. XML元素的名字,对应于Java类。
So a <TextView> element creates a TextView in your UI, and a <LinearLayout> element creates a LinearLayout view group. 所以一个<TextView>元素在你的UI中创建一个TextView,并且一个<LinearLayout>元素创建一个LinearLayout 视图组。
When you load a layout resource, the Android system initializes these run-time objects, corresponding to the elements in your layout.当你加载布局文件中元素所对应的资源时,Android系统初始化这些运行时对象
For example, a simple vertical layout with a text view and a button looks like this:比如,一个带有text 视图和button的垂直布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>
Notice that the LinearLayout element contains both the TextView and the Button. You can nest another LinearLayout (or other type of view group) inside here, to lengthen the view hierarchy and create a more complex layout.注意LinearLayout元素包括TextView和Button二者。你可以在里面嵌套另一个线性布局(或者其他类型的视图组),来扩展视图及创建更复杂的布局。
For more on building a UI layout, read XML Layouts. 更多关于构建UI布局,阅读XML 布局

Tip: You can also draw View and ViewGroups objects in Java code, using the addView(View) methods to dynamically insert new View and ViewGroup objects.提示:你可以用Java代码绘制视图和视图组,使用addView(View)可以动态插入新的View和ViewGroup对象。

There are a variety of ways in which you can layout your views. Using more and different kinds of view groups, you can structure child views and view groups in an infinite number of ways. 
Some pre-defined view groups offered by Android (called layouts) include LinearLayout, RelativeLayout, TableLayout, GridLayout and others. Each offers a unique set of layout parameters that are used to define the positions of child views and layout structure.
有许多视图布局的方法,使用多种View 组,你可以无穷尽的构建子视图和视图组。Android系统提供了一些预定义的视图组包括Linearlayout,RelativeLayout,Tablelayout,GridLayout及其他。每种布局都提供了一套独一无二的布局参数,这些参数用于定义子视图的位置和布局结构。
To learn about some of the different kinds of view groups used for a layout, read Common Layout Objects.要学习在布局中使用不同的视图组,参考Common Layout 对象。

Widgets小部件
A widget is a View object that serves as an interface for interaction with the user. Android provides a set of fully implemented widgets, like buttons, checkboxes, and text-entry fields, so you can quickly build your UI. Some widgets provided by Android are more complex, like a date picker, a clock, and zoom controls. But you're not limited to the kinds of widgets provided by the Android platform. If you'd like to do something more customized and create your own actionable elements, you can, by defining your own View object or by extending and combining existing widgets.
一个widget是一个View对象,服务于与用交互的界面。Android提供了一套完全实现的widgets,像buttons,checkboxes和text-entry fields,所以你可以快速的建立你的UI.Android提供了一些widgets有些得杂,比如像date picker,a clock及zoom控制。但你不会受限于Android系统提供的各种widgets.如果你喜欢做一些定制,创建你自的动作元素。你可以定义自己的View对象,或者结合存大的widgets进行扩展。
Read more in the Custom Components developer guide.阅读更多关于定制组件开发。
For a list of the widgets provided by Android, see the android.widget package.关于Android提供的widgets列表,查看 android.widget包。

Input Events输入事件

Once you've added some Views/widgets to the UI, you probably want to know about the user's interaction with them, so you can perform actions. To be informed of user input events, you need to do one of two things:
当你加入一些Views/widgets到UI后,你可能想知道用户与它们的交互,所以你能执行动作。为了接受到用户的输入事件,你需要处理以下两件事中的一件:
Define an event listener and register it with the View. More often than not, this is how you'll listen for events. The View class contains a collection of nested interfaces named On<something>Listener, each with a callback method called On<something>(). For example, View.OnClickListener (for handling "clicks" on a View), View.OnTouchListener (for handling touch screen events in a View), and View.OnKeyListener (for handling device key presses within a View). So if you want your View to be notified when it is "clicked" (such as when a button is selected), implement OnClickListener and define its onClick() callback method (where you perform the action upon click), and register it to the View with setOnClickListener(). 
定义一个事件监听器,并把它注册到View中。通常这是不需要的,这只是说你如何监听事件。View类包括一个嵌套接口集合叫做On<something>Listener,每个者带有一个回调方法叫做On<something>().比如, View.OnClickListener(用于处理View的”clicks”),View.OnTouchListener(处理触模事件)及 View.OnKeyListener (处理按键事件)。所以如果你想知道,当“clicked”时,你的View是否被通知到,实现OnClickListener并定义它的OnClick()回调方法(在此执行关于click的动作),并且用setOnClickListener注册该方法到View中。

Override an existing callback method for the View. This is what you should do when you've implemented your own View class and want to listen for specific events that occur within it. Example events you can handle include when the screen is touched (onTouchEvent()), when the trackball is moved (onTrackballEvent()), or when a key on the device is pressed (onKeyDown()). This allows you to define the default behavior for each event inside your custom View and determine whether the event should be passed on to some other child View. Again, these are callbacks to the View class, so your only chance to define them is when you build a custom component. 
Continue reading about handling user interaction with Views in the Input Events document.
复写视图存在的回访方法。这是你实现自己的view类和想监听对其发生的特定事件要做的事情。比如,你当屏幕被触模时可以处理(onTouchEvent()),当轨迹球移动时(onTrackballEvent()),或当一个键被按下时(onKeyDown()).这将允许你定义插入到你的View中的每个事件的默认行为,和是否将它传给其他的子视图。再次,有些View类的回调方法,所以你的唯一的机会来定义他们,当你建立你自己的组件时。在 Input Events 文档中继续阅读关于处理与用户的交互。

Menus菜单
Application menus are another important part of an application's UI. Menus offers a reliable interface that reveals application functions and settings. The most common application menu is revealed by pressing the MENU key on the device. However, you can also add Context Menus, which may be revealed when the user presses and holds down on an item.
应用菜单是应用UI中另一重要的部分。菜单提供显示应用功能和设置的可靠的界面。大多数应用菜单是通过按MENU键显示的。但是,你可增加上下文菜单,它可能是在用户按下,并按住时才显示。
Menus are also structured using a View hierarchy, but you don't define this structure yourself. Instead, you define the onCreateOptionsMenu() or onCreateContextMenu() callback methods for your Activity and declare the items that you want to include in your menu. At the appropriate time, Android will automatically create the necessary View hierarchy for the menu and draw each of your menu items in it.
菜单也是使用视图层次构建的,但是你不需要自己定义它。而是,为你的活动定义onCreateOptionsMenu() or onCreateContextMenu()方法,并声明在你的菜单中包括多少项。在适当的时候,Android系统将自动为你的菜单创建视图层次,并显示每一项的内容。
Menus also handle their own events, so there's no need to register event listeners on the items in your menu. When an item in your menu is selected, the onOptionsItemSelected() or onContextItemSelected() method will be called by the framework.
菜单也在处理它自己的事件,所以不需要在你的菜单中针对每一项注册监听器。当你的菜单的某项被选中时,框架将调用onOptionsItemSelected()方法和onContextItemSelected()方法。
And just like your application layout, you have the option to declare the items for you menu in an XML file.Read Menus to learn more.
并且,像你的应用布局一样,你也可以选择用XML文件声明你的菜单项。阅读Menus了解更多。

Advanced Topics高级话题
Once you've grappled the fundamentals of creating a user interface, you can explore some advanced features for creating a more complex application interface.一旦你熟悉了创建用户接口,你可以用一些高级特性,创建更加复杂的应用界面.

Adapters适配器
Sometimes you'll want to populate a view group with some information that can't be hard-coded, instead, you want to bind your view to an external source of data. To do this, you use an AdapterView as your view group and each child View is initialized and populated with data from the Adapter.
有时候,你想要插入一个视图组,它所带的信息不能用代码写死的,而时,把你的视图跟一些外部数据邦在一起。要实现这个,你要用到AdapterView做为你的视图组,并且每一个子视图被初始化并从适配器弹出数据。

The AdapterView object is an implementation of ViewGroup that determines its child views based on a given Adapter object. The Adapter acts like a courier between your data source (perhaps an array of external strings) and the AdapterView, which displays it. There are several implementations of the Adapter class, for specific tasks, such as the CursorAdapter for reading database data from a Cursor, or an ArrayAdapter for reading from an arbitrary array.
To learn more about using an Adapter to populate your views, read Binding to Data with AdapterView.
AdapterView对象是一个ViewGroup的实现,它基于Adapter对象来检测它的子视图。这个Adapter角色像是数据(也许是外部字串数组)与Adapter视图之间的一个courier。Adapter类针对特定的任务,有多个实现。比如从数据库读取数据的CursorAdapter,或者一个ArrayAdapter用于读取任意数组。阅读邦定数据到AdapterView,了解更多关于使用Adapter插入到你的视图。

Styles and Themes风格和主题
Perhaps you're not satisfied with the look of the standard widgets. To revise them, you can create some of your own styles and themes.也许你不满意标准的widgets.要修改它们,你可以创建一些自己的风格和主题

A style is a set of one or more formatting attributes that you can apply as a unit to individual elements in your layout.风格是一套一个或者多个格式化的属性,可以把它当成一个单元应用于你的布局元素。

 For example, you could define a style that specifies a certain text size and color, then apply it to only specific View elements. 比如,你可以定义对某些文本大小和顔色定义风格,然后用于特定的View元素。

A theme is a set of one or more formatting attributes that you can apply as a unit to all activities in an application, or just a single activity. 一个主题是一套一个或多个格式化的属性,可以把它当成一个单元用于应用的所有活动,或者单个活动。

For example, you could define a theme that sets specific colors for the window frame and the panel background, and sets text sizes and colors for menus. 比如,你可以定义一个主题,该主题指定了窗口边框的特定顔色和面板背景,及文本大小和菜单顔色。

This theme can then be applied to specific activities or the entire application. 这个主题可用于特别的活动或者所有的应用。
Styles and themes are resources. Android provides some default style and theme resources that you can use, or you can declare your own custom style and theme resources.
风格和主题是资源,Android提供了一些默认的风格和主题资源供你使用,或者你可以声明自己的自定义风格和主题资源。
Learn more about using styles and themes in the Styles and Themes document.在Styles和Themes文档中有更多关于风格和主题的使用。


posted on 2012-04-24 19:46  EarlyBird  阅读(728)  评论(0)    收藏  举报

导航