`

【翻译】(12)用户界面

 
阅读更多

【翻译】(12)用户界面

 

see

http://developer.android.com/guide/topics/ui/index.html

 

原文见

http://developer.android.com/guide/topics/ui/index.html

 

-------------------------------

 

User Interface

 

用户界面

 

In this document

 

本文目录

 

* View Hierarchy 视图层级

* Layout 布局

* Widgets 部件

* Input Events 输入事件

* Menus 菜单

* Advanced Topics 高级话题

* Adapters 适配器

* Styles and Themes 风格和主题

 

Key classes 

 

关键类

 

View

ViewGroup

Widget classes

 

-------------------------------

 

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 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类担当被称为部件的子类的基类,以提供完整实现的用户界面对象,像文本域和按钮。ViewGroup类作为被称为布局的子类的基类而服务,以提供不同类型的布局架构,像线性、表格(注:标签?)和相对布局。

 

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节点的层级来定义Activity对象的用户界面,正如以下图例演示的那样。这个层级树可以如你所需地简单或者复杂,你可以使用Android的预定义部件和布局集合构建它,或者使用你自己创建的自定义View类。

 

(图略:

ViewGroup -> View

          -> View

          -> ViewGroup -> View

                       -> View

                       -> View

 

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. 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.

 

为了把视图层级树依附到屏幕供渲染,你的Activity对象必须调用setContentView()方法并传递引用到根节点对象。Android系统接收到这个引用并使用它无效化(注:这里invalidate指重新画屏),度量和绘画视图树。层级的根节点要求它的子节点绘画它们自己——同样,每个视图组节点负责叫它自己的每个子视图去绘画它们自己。子对象可以在父对象中请求大小和位置,但父对象拥有每个子对象可以在哪里以及有多大的最终决定权。Android按顺序解析布局的元素(从层级树的顶部开始),实例化View对象并添加它们到它们的父对象。因为这些对象是按顺序绘画的,如果有元素重叠了位置,那么最后一个被绘画的对象将处于其它之前被绘画在那个空间的对象的上方。

 

For a more detailed discussion on how view hierarchies are measured and drawn, read How Android Draws Views.

 

想获取关于视图层级如何被度量和绘画的更详细讨论,请阅读Android绘画视图章节。

 

-------------------------------

 

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).

 

定义你的布局并表现视图层级的最通常的方式是使用XML布局文件。XML为布局提供易于让人阅读的结构,非常类似于HTML。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>元素在你的用户界面中创建一个TextView对象,而<LinearLayout>元素创建一个LinearLayout视图组。当你加载一个布局资源时,Android系统初始化这些运行时对象,对应于你的布局内的元素。

 

For example, a simple vertical layout with a text view and a button looks like this:

 

例如,一个带有文本视图和按钮的简单垂直布局看起来就像这样:

 

-------------------------------

 

<?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。你可以在它里面嵌套另一个LinearLayout(或另一种视图组),以延伸视图的层级和创建更复杂的布局

 

For more on building a UI layout, read XML Layouts.

 

想获取更多关于构建用户界面布局的信息,请阅读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.

 

提示:你还可以使用addView(View)方法动态地插入新的View和ViewGroup对象,在Java代码中绘画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.

 

你可以用不同的方式布局你的视图。使用更多和不同种类的视图组,你可以以无限数量的方式构建子视图和视图组。一些由Android提供的预定义视图组(称为布局)包括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.

 

部件是View对象,担当与用户交互的界面。Android提供一系列完全实现的部件,如按钮、勾选框,和文本输入域,所以你可以快速构建你的用户界面。一些由Android提供的部件会更复杂,如数据拾取器、时钟、缩放控件。但你并不限于由Android平台提供的部件类型。如果你想处理更多自定义的事情并创建你自己的可操作元素,那么你可以通过定义你自己的View对象或通过扩展和组合现存部件来做到。

 

Read more in the Custom Components developer guide.

 

更多信息在自定义组件开发者指引中。

 

For a list of the widgets provided by Android, see the android.widget package.

 

想获取Android提供的部件列表,请参见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:

 

一旦你添加一些视图/部件到用户界面,你很可能希望知道用户对它们的交互,使你能执行一些动作。为了被用户输入事件通知,你需要做两件事情的其中一件。

 

* 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<动作>Listener的接口类,每个类的活动方法叫On<动作>()。例如,View.OnClickListener(用于处理View上的“点击”),View.OnTouchListener(用于处理View中触碰屏幕事件),以及and View.OnKeyListener(用于处理View中设备键盘按下)。所以如果你希望你的View在它被“点击”时被通知(诸如当按钮被选中),那么请实现OnClickListener类并定义它的onClick()回调方法(在那里你执行点击时的动作),并用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.

 

* 覆盖View的现存回调方法。当你实现你自己的View类并希望监听发生在它里面的特定事件时,你应该这样做。你可以处理的例子事件包括当你屏幕被触碰(onTouchEvent()),当轨迹球被移动(onTrackballEvent()),或当设备上的键盘被按下(onKeyDown())。这允许你定义你的自定义View内每个事件的默认行为,并且决定事件是否被传递给其它一些子View类。再一次重申,这些是View类的回调,所以你定义它们的唯一机会是在你构建一个自定义组件的时候。

 

Continue reading about handling user interaction with Views in the Input Events document.

 

请在输入事件文档中继续阅读关于用View处理用户交互的信息。

 

-------------------------------

 

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.

 

应用程序菜单是应用程序用户界面的另一个重要部分。菜单提供展示应用程序功能和设置的可靠界面。大多数一般的应用程序菜单通过按设备上的菜单键来展开。然而,你还可以添加上下文菜单,它可以在用户按下不放开一个条目时展开。

 

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.

 

菜单还可以用View层级进行构建,但你不能自己定义此结构。取而代之的是,你为你的Activity类定义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 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.

 

同时,正如你的应用程序布局那样,你可以选择在XML文件中声明菜单的条目。

 

Read 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.

 

有时,你希望用一些不能硬编码的信息填充视图组,相反,你希望绑定你的数据到外部源数据。为了做到这一点,你可以使用AdapterView作为你的视图组,每个子视图使用来自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的实现类,基于给定的Adapter对象决定其子视图。Adapter的行为就像你的数据源(可能是一个外部字符串数组)和显示它的AdapterView之间的信使。针对特定的任务,存在几个Adapter类的实现,诸如CursorAdapter用于从Cursor对象中读取数据库数据,或ArrayAdapter用于从任意数组中读取数据。

 

To learn more about using an Adapter to populate your views, read Binding to Data with AdapterView.

 

请阅读用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.

 

可能你对标准部件的外观不满。为了修改它们,你可以创建一些你自己的风格和主题。

 

* 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.

 

请在风格和主题文档中学习更多关于使用风格和主题的信息。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 17 Nov 2011 21:59

Site Terms of Service - Privacy Policy - Brand Guidelines

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)


分享到:
评论

相关推荐

    软件测试-用户界面检查表

    用户界面(UI)是软件与用户交互的桥梁,它的质量直接影响到软件的用户体验和整体功能的可用性。软件测试中的用户界面检查表是一项关键任务,旨在确保UI设计满足用户需求和行业标准。以下是对该检查表中涉及的知识点...

    FLUENT界面中文翻译

    ### FLUENT界面中文翻译及命令详解 #### 1. 常规设置 Fluent 的常规设置主要包括软件的基本配置,比如语言环境、单位系统、数值精度等。这些设置为后续的操作提供了一个基本的框架。 #### 2. 模型设置 在 Fluent ...

    U3D中文界面翻译

    ### U3D中文界面翻译详解 #### Unity3D菜单与参数中文翻译及功能解析 **一、File(文件)** 1. **New Scene**:新建场景。用于创建一个新的空白场景,用户可以在其中添加游戏对象、组件等。 2. **Open Scene**:...

    STM32 CubeMx 界面翻译

    ### STM32 CubeMx 界面翻译及配置详解 #### ADC Configuration (ADC配置) **Parameter Settings (参数设置)** - **Clock Prescaler (时钟分频):** 用于设置ADC时钟频率与APB2时钟之间的比例,常见的选项包括: -...

    FLUENT15.0基础界面中文翻译

    本知识点主要介绍FLUENT 15.0的基础界面中文翻译,帮助中文用户更快上手这款软件。 1. 常规设置 常规设置是整个CFD分析的第一步,包括问题定义、总体控制和输出设置等。用户需要在这里定义解算器类型、操作条件、...

    moho12汉化方法包 Anime Studio12中文版汉化文件下载

    Anime Studio 12是一款深受动画制作爱好者喜爱的专业2D动画软件,它以其强大的功能和直观的用户界面,为创作者提供了丰富的工具集,用于制作高质量的动画作品。然而,对于中文用户来说,原版软件的英文界面可能带来...

    外文翻译共12页.pdf.zip

    在IT领域,外文翻译可能是指将技术文档、软件界面、网站内容等从一种语言转换为另一种语言。现代翻译工具如Google Translate、DeepL等利用机器学习和神经网络技术自动进行翻译,但专业的人工翻译仍不可替代,尤其是...

    fluent15.0界面中文翻译

    首先,介绍Fluent 15.0界面的中文翻译,可以方便中文用户更容易理解和操作这款强大的计算流体动力学(CFD)软件。下面将一一解析各个翻译内容对应的英文术语和中文翻译内容: 1. 常规设置(General Settings) - ...

    计算机专业术语50个翻译.pdf

    GNOME是一个基于Linux平台的桌面环境,提供了图形用户界面和桌面管理功能。它是一个开源的桌面环境,支持多种硬件平台。 5.GNU GNU's Not Unix (GNU 不是 Unix) GNU是一个开源的操作系统,提供了 Unix 风格的操作...

    Exa6-翻译小助手

    这款软件的主要功能是帮助开发者、设计师以及项目团队在进行信息技术(IT)和用户界面(UI)相关工作时,更轻松地进行语言翻译和沟通。下面将详细阐述这款工具的特点、应用场景以及可能涉及的技术知识点。 1. **多...

    界面设计规范

    - 针对目标用户群体的需求定制界面元素。 - 利用现成的专业工具库或框架加速开发进程。 #### 二、提示信息规范 **1. 提示文本的明确性:** - **描述:** 提示文本应清晰明了,避免模棱两可的表述。 - **实践建议...

    JAVA 翻译文献 中英文全 翻译过来汉字超4000

    10. **JAVA库和API**:如Swing和JavaFX用于构建图形用户界面,JDBC用于数据库连接,JavaMail API用于发送邮件等。 11. **JAVA EE(Enterprise Edition)**:用于开发企业级应用程序,包括Servlet、JSP、EJB等技术。...

    matlab代码翻译.pdf

    MATLAB提供了图形用户界面的工具,可以通过 `uicontrol`、`uimenu` 等函数创建用户界面组件。 11. 高级数学函数 - `exp`:指数函数。 - `log`:自然对数。 - `sin`、`cos`、`tan` 等三角函数。 - `sqrt`:平方根。 ...

    Android Application Fundmentals及其翻译5000字(安卓毕设外文及翻译)

    5. **布局与视图**:XML用于定义应用的用户界面,包括各种布局(如LinearLayout、RelativeLayout、ConstraintLayout等)和视图元素(TextView、Button、ImageView等)。 6. **数据存储**:Android提供多种数据存储...

    PLSQL12完美汉化包

    1. **用户界面**:汉化后的用户界面使得中国用户可以更加直观地理解各个菜单、选项和提示,提升了用户体验。包括工作区、工具栏、菜单栏、快捷键等元素都进行了全面的中文翻译。 2. **代码编辑器**:PLSQL ...

    JAVA课程设计题目

    知识点:图形用户界面编程、词典编程、翻译系统设计。 13. 加密与解密:采用图形用户界面,给定任意一个文本文件,进行加密,生成另一个文件,对加密后的文件还原。 知识点:图形用户界面编程、加密算法、解密算法...

    SCITranslate12.rar

    - **学习与改进**:随着用户的使用,SCITranslate12能够不断学习并优化翻译算法,提升翻译质量。 2. **使用方法** - **安装与启动**:首先解压"SCITranslate12.rar",然后按照安装向导进行安装。安装完成后,在...

    电脑BIOS界面英文翻译.doc

    电脑BIOS界面英文翻译 BIOS(Basic Input/Output System,基本输入/输出系统)是被固化在计算机CMOS RAM芯片中的程序,为计算机提供最初的、最直接的硬件控制。BIOS 主要有两类:AWARD BIOS 和 AMI BIOS。正确设置 ...

    Android中文翻译组——Android中文合集(6)

    3. **Activity与Intent**:Activity是Android中的用户界面组件,用于展示用户界面并处理用户交互。Intent则是Android系统中用于组件间通信的机制,它可以启动或启动新的Activity,或者在已有Activity之间传递数据。 ...

    qt creator 实现 语言切换

    5. **设计用户界面**: 为了让用户能够方便地切换语言,我们可以在界面上添加一个按钮,当点击该按钮时调用`switchLanguage()`函数,并传递相应的语言代码,如"zh_CN"或"en_US"。 6. **部署和运行**: 在部署应用...

Global site tag (gtag.js) - Google Analytics