User Interface用户界面
Key classes
View
ViewGroup
Widget classes
In this document
- View Hierarchy
- Layout
- Widgets
- UI Events
- Menus
- Advanced Topics
- Adapters
- Styles and Themes
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对象建立。view和view group有很多种,均为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对象是用户界面的基本单元。View类是widget的父类,widget为系统内置ui对象,如文本框和按钮等等。ViewGroup类为layout类的父类,layout为不同种类的布局结构,例如线型、表格型和相对型。
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对象处理它自己的测量、布局、绘画、焦点变化、卷屏以及它所处的矩形区域内的键盘/手势交互。作为UI中的一个对象,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组成的层次结构来定义的一个Activity的UI,如下图所示。这个层次树可以简单也可以复杂,你可以使用Android预定义的widget和layout,也可以用你自己自定义的View来构建它。
视图组
In order to attach the view hierarchy tree to the screen for rendering, your Activity must call the
method and pass a reference to the root node object. The Android system receives this reference and uses it to invalidate, measure, and draw the tree. 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). 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.setContentView()
为将View层次树显示在屏幕上,你的 Activity需要使用setContentView()方法,并将根节点对象传递给他。Android系统接收到这个引用并用它来清除、测量和绘制这 个树。根节点要求它的子节点能够绘制自己——每个组节点负责它自己的子view来绘制自己。子View可能会从父View处获得大小和位置,但父对象最后 决定子对象的大小。Android解析layout,初始化View并把它们加入它们的父View。由于它们按顺序绘制,如果这些元素相互重叠,最后一个 被绘制的元素将位于最上层。”How Android Draws Views”介绍了绘制的细节。
For a more detailed discussion on how view hierarchies are measured and drawn, read 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 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). View objects are leaves in the tree, ViewGroup objects are branches in the tree (see the View Hierarchy figure above).
最常见的定义layout和表示view结构的方法就是使用一个xml layout文件。xml提供了一个人类可读的结构。xml的每个元素是一个View或者ViewGroup对象。View对象为树中的树叶,ViewGroup对象为树中的树枝。
The name of an XML element is respective to the Java class that it represents. So a <TextView>
element creates a TextView
in your UI, and a <LinearLayout>
element creates a LinearLayout
view group. When you load a layout resource, the Android system initializes these run-time objects, corresponding to the elements in your layout.
xml 元素的名称和它的java类相对应。因此一个元素创建一个TextView对象,一个 元素创建一个LinearLayout的view group。当你加载一个layout资源时,Android系统会基于layout中的元素来初始化这些运行时对象。
For example, a simple vertical layout with a text view and a button looks like this:
例如,一个简单的竖排布局,中间有一个text view和一个按钮,像这样:
1 <? xml version =“1.0″ encoding =“utf-8″ ?>
2 < LinearLayout xmlns : android =“http://schemas.android.com/apk/res/android “
3 android : layout_width =“fill_parent”
4 android : layout_height =“fill_parent”
5 android : orientation =“vertical” >
6 < TextView android : id =“@+id/text”
7 android : layout_width =“wrap_content”
8 android : layout_height =“wrap_content”
9 android : text =“Hello, I am a TextView” />
10 < Button android : id =“@+id/button”
11 android : layout_width =“wrap_content”
12 android : layout_height =“wrap_content”
13 android : text =“Hello, I am a Button” />
14 </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。你可以在这个LinearLayout中使用另一个LinearLayout,来 创建更复杂的布局。 For more on building a UI layout, read Declaring Layout .
addView(View)
methods to dynamically insert new View and ViewGroup objects.
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。使用不同的View group,你可以让View和viewgroup组织为任意的形式。一些预定义的Viewgroup有:LinearLayout, RelativeLayout, TableLayout, GridLayout等。
To learn about some of the different kinds of view groups used for a layout, read Common Layout Objects .
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提供了一系列已经定义好的widget,例如按钮,复选框以及文本框等。还有一些更复杂的widget,例如日期选择器,始终以及缩放控制器等。你也可以定义自己的widget。
Read more in Building Custom Components . For a list of the widgets provided by Android, see the android.widget
package.
UI 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 UI events, you need to do one of two things:
一旦你在UI中加入View和widget,你可能希望知道用户对他们的操作,这样你就可以进行一些动作。为了得到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), andView.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 itsonClick()
callback method (where you perform the action upon click), and register it to the View with
.setOnClickListener()
- 定义一个event listener并在View中注册它。 通 常这是你监听事件的方法。View类包含一系列嵌套的on<xxx>Listener接口,每个都有一个on<xxx>()回调 函数。例如View.OnClickListener(处理View上的点击事件),View.OnTouchListener(处理触摸屏事 件),View.OnKeyListener(处理键盘事件)。因此如果你希望你的View处理点击事件,则定义OnClickListener的 onClick()方法并使用setOnClickListener()来注册它。
- 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 (
), when the trackball is moved (onTouchEvent()
), or when a key on the device is pressed (onTrackballEvent()
). 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 .onKeyDown()
- 定义一个event listener并在View中注册它。 通 常这是你监听事件的方法。View类包含一系列嵌套的on<xxx>Listener接口,每个都有一个on<xxx>()回调 函数。例如View.OnClickListener(处理View上的点击事件),View.OnTouchListener(处理触摸屏事 件),View.OnKeyListener(处理键盘事件)。因此如果你希望你的View处理点击事件,则定义OnClickListener的 onClick()方法并使用setOnClickListener()来注册它。
Continue reading about handling user interaction with Views in the Handling UI Events document.
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键呼出的。你也可以使用长按屏幕上某个元素的方式来呼叫出上下文菜单(Context Menu)。
Menus are also structured using a View hierarchy, but you don’t define this structure yourself. Instead, you define the
or onCreateOptionsMenu()
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.onCreateContextMenu()
菜单也是用View层次来组织的,但是不要自己定义这个结构。使用onCreateOptionsMenu()和onCreateContextMenu()回调函数并定义菜单的内容。Android会自动建立需要的View结构并绘制其内容。
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
or onOptionsItemSelected()
method will be called by the framework.onContextItemSelected()
菜单也需要处理他们的事件,因此不需要注册事件监听器(event listeners)。当菜单项被选择时,onOptionsItemSelected() 或 onContextItemSelected()方法会被调用。
And just like your application layout, you have the option to declare the items for you menu in an XML file.
你也可以在xml中定义菜单的项。 Read Creating Menus to learn more.
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.
有时候你希望给view group加入动态元素,这些元素不能硬编码,而是需要和外部数据相绑定。为了实现这一点,你需要用AdapterView作为view group,每一个子view使用adapter来进行初始化。
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.
AdapterView 是ViewGroup的一个实现,该view group使用一个给定的Adapter对象来确定它的子View。Adapter作为数据源(例如一个字符串数组)和AdapterView(负责显 示)中的一个联系人。有好几种特殊的Adapter实现,例如CursorAdapter(用于从一个Cursor读取数据库数据)和 ArrayAdapter(用来从任意数组中读取数据)。
To learn more about using an Adapter to populate your views, read Binding to Data with AdapterView .
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.
可以用样式和主题来自定义标准widget的外观。
- 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.
- 一个样式是一个或多个格式属性的集合,这些格式属性可以被作为独立的元素放在layout中。例如你可以定义一个样式,该样式制定了某种字体大小和颜色,并将它制定给某几个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.
- 一个主题是一个或多个格式属性的集合。这些属性可以对一个应用程序中所有的或一个activity使用。例如,你可以定义一个主题,制定了窗口框的颜色和面板北京颜色,以及菜单的字体字号等。一个主题可以被应用于某些activity或者整个应用程序。
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 Applying Styles and Themes document. from:http://androidappdocs.appspot.com/guide/topics/ui/index.html