[Learn Android Studio 汉化教程]第二章:Android Studio概述(一)

[Learn Android Studio 汉化教程]第二章:Android Studio概述(一)

Android Studio is a windowed environment. To make the best use of limited screen real-estate, and to keep you from being overwhelmed, Android Studio displays only a small fraction of the available windows at any given time. Some of these windows are context-sensitive and appear only when the context is appropriate, while others remain hidden until you decide to show them, or conversely remain visible until you decide to hide them. To take full advantage of Android Studio, you need to understand the functions of these windows, as well as how and when to display them. In this chapter, we’re going to show you how to manage the windows within Android Studio.

<翻译> Android Studio是一个视窗化的开发环境。为了充分利用有限的屏幕空间,不让你束手束脚,
Android Studio 在特定的时间仅仅显示一小部分可用窗口。除了一些上下文敏感的窗口和上下文相关的窗口显示出来外,其他的仍旧隐藏,除非你主动打开它们。或者相反,一些可见的窗口直到你主动隐藏它们。为了充分利用Android Studio,你就需要明白这些窗口的功能以及如何、何时去显示它们。在这一章,我们将向你展示如何管理Android Studio的窗口。

One of the essential functions of any integrated development environment (IDE) is navigation. Android projects are typically composed of many packages, directories, and files, and an Android project of even modest complexity can contain hundreds of such assets. Your productivity with Android Studio will depend in large measure on how comfortable you are navigating within these assets and across them. In this chapter, we’re also going to show you how to navigate in Android Studio.

<翻译> 任何一个集成开发环境(IDE)的基础功能就是导航。Android 项目通常是由许多的包,目录和文件组成,甚至一个稍微复杂的Android项目能够包含数百个这样的资源。你使用Android Studio的效率很大程度上取决于你处理这些资源的舒适程度。在这一章,我们还将展示在Android Studio中如何使用导航。

Finally, we’ll show you how to use the help system within Android Studio. To take full advantage of this chapter, open the HelloWorld project we created in Chapter 1. If this project is already open in Android Studio, you’re ready to go. Please refer to Figure 2-1 as we discuss the following navigation features.

<翻译> 最后,我们将向你展示如何使用Android Studio中的帮助系统。为了充分理解这一章,我们打开第一章创建的HelloWorld项目。如果项目已经在Android Studio中打开,说明你已经准备好了。请参考图2-1,我们将讨论下面的导航功能。


Figure 2-1. Android Studio’s integrated development environment
<翻译>  图2-1 Android Studio集成开发环境

The Editor

编辑器

The primary purpose of any IDE is to edit files. As one would expect, the window that allows users to edit files in Android Studio is located in the center pane of the IDE. The Editor window is unique among windows in that it is always visible and always located in the center pane. In fact, the Editor window is such a pervasive feature of Android Studio that from here on out, we refer to it simply as the Editor. All the other windows in Android Studio are called tool windows and cluster in side panes (left, bottom, and right) around the Editor.

<翻译> 任何IDE的主要目的就是编辑文件。正如人们所预料的,在Android Studio中允许用户编辑文件的窗口位于IDE的中心。在所有的窗口中,编辑窗口是唯一一个一直可见并位于中心的窗口。事实上,编辑窗口在Android Studio中无处不在,所以从现在起我们称它为编辑器。所有在Android Studio中围绕在编辑器周围的其他窗口都被称为工具窗口和侧窗口(左部,底部,右部)。

The Editor is a tabbed window, and in this respect it resembles a contemporary web browser. When you open a file from one of the tool windows, from a keyboard shortcut, or from a context menu, the file displays as a tab of the Editor. As you already discovered when you built your first project, HelloWorld, the MainActivity.java and the activity_main.xml files were automatically loaded in the Editor as tabs. Android Studio tries to anticipate which files you’re likely to start editing, and then opens them automatically as tabs in the Editor upon completion of the New Project Wizard. Virtually any file may be opened in the Editor, though raw image and sound files cannot (yet) be edited from within Android Studio. You may also drag and drop a file from a tool window onto the Editor; doing this opens the file as a tab in the Editor

<翻译> 编辑器是一个类似于现代WEB浏览器的选项卡式的窗口。当你从工具窗口,键盘快捷键或上下文菜单打开一个文件的时候,文件将作为一个选项卡显示在编辑器中。再你新建的第一个项目——HelloWorld中,MainActivity.java和activity_main.xml作为选项卡自动加载在编辑器中。Android Studio试图揣测你想开始编辑哪些文件,然后在新项目向导完成后自动作为选项卡在编辑器中打开。几乎任何文件能够在编辑器中打开,虽然原始图像和声音文件还不能在Android Studio中编辑。你也可以从工具窗口拖拽文件到编辑器,这样做可以直接作为选项卡在编辑器中打开。

Along the top of the Editor are the Editor tabs. Along the left margin of the Editor is the gutter, and along the right margin of the Editor is the marker bar. Let’s examine each in turn.

<翻译>
编辑器上面是编辑选项卡。在左边沿是边列,在右边沿是编辑栏。让我们开始探索吧。

Editor Tabs

编辑选项卡

To navigate among Editor tabs in Android Studio, use the Alt+Right-Arrow | Ctrl+Right-Arrow or Alt+Left-Arrow | Ctrl+Left-Arrow keys. Of course, you may always select an Editor tab with your mouse. The Editor tabs’ options are located in the main menu bar at Window ➤ Editor Tabs. Any action you select from this menu applies to the currently selected tab. Roll your mouse over the MainActivity.java tab and right-click (Ctrl-click on Mac) it. In the resulting context menu, shown in Figure 2-2, you will notice many of the same options that you discovered in Window ➤ Editor Tabs. From this context menu, select the Tabs Placement submenu. The menu options Top, Bottom, Left, and Right allow you to move the tabs bar. Moving the tabs bar to the right or left accommodates more visible tabs, though at the expense of screen real-estate.

<翻译>在Android Studio中切换选项卡可以使用Ctrl或Alt+左或右箭头。当然,你也可以用鼠标选择编辑选项卡。编辑器选项卡的选项位于主菜单栏的Window>Editor Tabs下。你在此处进行的任何操作都将应用于当前选中的选项卡上。将你的鼠标放置在mainActivity.java选项卡并右击它(Mac下按住Ctrl点击)。如图2-2所示,在生成的菜单中,你会看到很多跟Window>Editor Tabs下相同的选项。从这个菜单中,选择Tabs Placement子菜单。此菜单选项允许你将选项卡移动到上,下,左,右。将选项卡移动到左边或右边能容纳更多可见的选项卡,但是会占用部分屏幕。



Figure 2-2. Editor tab context menu
<翻译>  图2-2 内容菜单编辑选项卡

The Close and Close All actions of the Editor tab context menu are straightforward. The Close Others action is used when you want to close all the tabs except the active tab. The Split Vertically and Split Horizontally actions are used to subdivide your Editor into panes. Split Vertically is particularly useful if you want to compare two files side by side. You may split panes ad infinitum, though the utility of such nested splits quickly diminishes. You may also drag and drop files from other windows to any pane of the Editor, or from one pane to another. Closing the last tab of a pane causes the entire pane to disappear.

<翻译>编辑器菜单上的Close和Close All操作是很明确的。而你想关闭除此之外的所有选项卡,你应该使用Close Others。而Split Vertically和Split Horizontally操作用来将编辑器细分成窗口。如果你想比较两个文件那么Split Vertically将会很有用。你可以无止境的进行细分,尽管这种嵌套分割的效果大大降低。你也可以将其他窗口的文件拖拽到编辑器的任何面板,或者从一个面板到另一个面板。当最后一个选项卡关闭的时候,整个面板就会消失。

The Gutter

边列

The gutter is used to convey information about your code. Perhaps the most obvious feature of the gutter is that small color swatches and image icons are displayed there alongside corresponding lines of code that refer to those visual resources. The gutter is also used to set breakpoints, facilitate code-folding, and display scope indicators. All of these features are covered in more detail in subsequent sections and chapters.

<翻译>边列用于传递你代码的信息。也许边列最明显的特性就是在你代码的同一行用小色卡或者小图标来显示那些可视化资源。边列也可以用于设置断点,支持代码折叠,并用作代码范围指示器。所有这些特性将会在后续的章节中详细介绍。

The Marker Bar

标记栏

Along the right side of the Editor is the marker bar. The marker bar is used to indicate the location of important lines in your source files. For example, the marker bar highlights warnings and compile-time errors in your Java or XML files. The marker bar also shows you uncommitted changes, search results, and the locations of bookmarks. The marker bar does not scroll like the gutter does; rather, the colored ticks on the marker bar are positioned relative to the length of the file. Clicking a colored tick in the marker bar immediately jumps you to that location in the file. Practice using the marker bar by clicking some of its colored ticks now.

<翻译> 编辑器的右边是标记栏。标记栏用于指示你资源文件中重要行的位置。例如,当你的Java或XML文件出现警告信息或编译出错时标记栏会高亮显示这些信息。右边栏也会显示未提交的更改,搜索的结果和书签的位置。标签栏不会像边列一样滚动,相反,标签栏上的彩色标记是文件长度的相对定位。点击标签栏的彩色标记将会立即跳转到文件的位置。现在通过点击一些彩色标记来练习使用标签栏。

Tool Buttons

工具按钮

You’ve already seen the Project tool window, displayed in the left pane by default. To see a list of all the tool windows, choose View ➤ Tool Windows from the main menu bar. Now look carefully along the left, right, and bottom margins of the IDE. There you will find tool buttons that correspond to many of the tool windows. Notice that some of these tool buttons are also labeled with a number, which is used in combination with the Alt (Cmd on Mac) key to toggle that tool button’s corresponding tool window open/closed. Experiment with clicking the tool buttons now to practice this skill. Also practice using the keyboard shortcuts Alt+1 | Cmd+1, Alt+2 | Cmd+2, Alt+3 | Cmd+3, and so forth to toggle the tool windows open/closed.

<翻译>你应该已经看到了默认显示在左边面板的项目工具窗口。你可以点击主菜单上的View>Tool Windows来查看全部的工具窗口。现在你可以仔细的观察IDE的左边,右边和下边。你会发现工具按钮相对应的窗口。注意,有一些工具按钮后面有一个数字,这些数字结合Alt(Mac上的Cmd)键用来打开/关闭相应的工具窗口。尝试点击这些工具按钮来练习这项技能。同时使用键盘快捷键Alt+1|Cmd+1,Alt+2|Cmd+2,Alt+3|Cmd+3等打开/关闭工具窗口。

When a tool window is open, the corresponding tool button is dark gray, indicating that it is depressed. Notice that the tool buttons are located in the corners of the margins. For example, the default position of the Project tool button is in the upper corner of the left margin, while the Favorites tool button is located by default in the lower corner of the left margin.

<翻译>当某个工具窗口打开之后,相应的工具按钮呈深灰色,表示不能按下。请注意位于边缘角落的工具按钮。例如,Project的默认位置在左上角,Favorites的默认位置在左下角。

Side panes (left, bottom, and right) may be shared by up to two tool windows at a time. To see how side panes may be shared, open both the Favorites and the Project tool windows. Notice that the Favorites and Project tool buttons are located in opposing corners of the same margin. Attempting to share a side pane between two tool windows whose corresponding tool buttons are located in the same corner will not work. For example, the Project and Structure tool windows cannot be displayed simultaneously—at least not in Android Studio’s default configuration.

<翻译>侧边栏(左边,下边,右边)可以同时被两个工具窗口共享。同时打开Favotites和Project工具窗口看看侧边栏是如何共享的。可以看到,Favorites和Project工具窗口位于同一边的相对的角落。两个位于相同角落的工具窗口是不会同时出现在一个侧窗格窗口的。例如,Project和Structure工具窗口不能够同时显示出来——至少在Android Studio的默认配置上不会。

Default Layout

默认布局

Don’t confuse default layout in Android Studio with layouts in the Android SDK. A default layout is a specific set of tool windows clustered around the Editor. Android Studio is configured out-of-the-box with a default layout that shows the Project tool window in the left pane. This is the layout displayed previously in Figure 2-1.

<翻译>不要将Android Studio中的默认布局和Android SDK中的布局混为一谈。默认布局是聚集在编辑器周围的一组特定的工具窗口。Android Studio打开之后配置一个将Project工具窗口显示在左边的默认布局。以前布局显示如图2-1所示。

Let’s examine the Window menu in the main menu bar. The first two menu items are Store Current Layout as Default, and Restore Default Layout. The Restore Default Layout action is typically used when the IDE becomes overcrowded, or you just want to clear the slate and return to a familiar layout. You may also customize your default layout by opening and closing whichever tool windows you like, resizing and/or repositioning them, and then setting that new layout as the default by selecting Store Current Layout as Default.

<翻译>让我们看看主菜单上的Window菜单。前两个菜单项是Store
Current Layout as Default(将当前布局存储为默认布局)和Restore Default Layout(恢复默认布局)。恢复默认布局通常用在IDE窗口变得拥挤,或者你想清理面板将之还原到你熟悉的布局。你也可用通过打开,关闭,调整或者重新定位你喜欢的窗口,然后选择将当前布局存储为默认布局来设置新的默认布局。

—————————————————————————————————————————————————
REPOSITIONING TOOL BUTTONS
重新定位工具按钮


As mentioned, the Project and Structure tool-windows can’t be displayed simultaneously because their corresponding tool-buttons are located in the same corner. However, you can move any tool-button to any corner you want. Drag-and-drop the Structure tool button to the bottom corner of the left margin. Now, toggle the Project and Structure tool-windows open by using either the keyboard shortcuts Alt+1 | Cmd+1 and Alt+7 | Cmd+7 or by clicking their tool buttons. Because we moved their tool buttons to opposing corners, the Project and Structure tool windows may now share the same side pane and be displayed simultaneously.

<翻译>正如前面所提到的,Project和Structure工具窗口之所以不能够同时显示,是因为他们相应的工具按钮位于相同的角落。但是你可以任意移动工具按钮到你想要的角落。将Structure工具按钮拖拽到左下角的角落。现在,使用键盘快捷键Alt+1|Cmd+1和Alt+7||Cmd+7或者点击工具按钮来打开Project和Structure工具窗口。由于我们将这两个工具按钮移动到相反的角落,所有Project和Structure能够在相同的面板窗口同时地展现出来。
—————————————————————————————————————————————————

Navigation Tool Windows

导航工具窗口

This section discusses tool windows that are used specifically for navigation: Project, Structure, Favorites, TODO, and Commander. Table 2-1 lists the function of each of these navigation tool windows. Subsequent chapters cover many of the other tool windows.

<翻译>本节讨论专门用作导航的工具窗口:Project,Structure,Favorites,TODO和Commander。表2-1列出了他们每个导航窗口的功能。之后的章节将讨论许多其他工具窗口。

Table 2-1. Navigation Tool Windows


表2-1 导航工具窗口


The Project Tool Window

项目工具窗口

We find the Project tool window to be the most useful of navigation tool windows because it combines wide scope with relatively easy access. To appreciate the power and scope of the Project tool window, you may want to set the window’s mode to Project. There are three modes; Project, Packages, and Android. By default, Android Studio will set the mode to Android. Android and Project are the most useful modes, though the Android mode may hide certain directories from you. The mode setting combo-box is located at 90 degrees and adjacent to the Project tool button in the upper left corner of the IDE. The Project tool window provides a simple tree interface with files and nested directories that you can toggle. The Project tool window gives you an overview of all the packages, directories, and files in your project. If you right-click (Ctrl-click on Mac) a file in the Project tool window, a context menu appears. There are three important menu items in this context menu: Copy Path, File Path, and Show in Explorer. Clicking Copy Path copies the operating system’s absolute path to this file to the clipboard. Clicking File Path displays the path as a stack of directories, terminating with the file on top, and clicking any of these directories opens them in the operating system. Clicking Show in Explorer shows the file in a new window of your operating system. See Figure 2-3.

<翻译>我们发现Project工具窗口是非常有用的导航工具窗口,因为它将有限的宽度空间和相对容易的访问结合起来。为了欣赏Project工具窗口的效用,你要将Project设置为窗口模式。Project总共有三种模式;Project,Packages和Android。Android Studio在默认情况下将模式设置为Android。Android和Project是非常有用的模式,即使Android模式可能为你隐藏某些目录。模式设置组合框毗邻IDE的左上角Project按钮并与之呈90度角。Project工具窗口提供了一个简单的嵌套接口文件和目录树来方便你切换。Project工具窗口呈现你项目中的所有包,目录和文件的概览。如果你在项目工具窗口中右击(mac下按住ctrl单击),将会显示一个上下文菜单。此处有三个重要的菜单项:复制路径,文件路径和在资源管理器中显示。点击Copy Path复制操作系统的绝对路径到剪贴板。点击File Path以倒序方式显示目录堆栈,点击任何一个目录都将在操作系统中打开他们。点击 Show in Explorer将会在你的操作系统中打开一个新窗口来显示文件。如图2-3.


Figure 2-3. The Project tool window
<翻译>  图2-3 项目工具窗口

The Structure Tool Window

结构工具窗口

The Structure tool window displays a hierarchy of elements in your file. When the Editor is displaying a Java source file such as MainActivity.java, the Structure tool window displays a tree of elements such as fields, methods, and inner classes. When the Editor is displaying an XML file such as activity_main.xml, the Structure tool window displays a tree of XML elements. Clicking any element in the Structure tool window immediately moves your cursor to that element in the Editor. The Structure tool window is particularly useful for navigating among elements in large source files. Practice this skill by opening the Structure tool window and navigating among the elements of both MainActivity.java and activity_main.xml. See Figure 2-4.

<翻译>结构工具窗口用来显示文件中元素的层次结构。当编辑器显示诸如MainActivity.java的java源文件时,Structure工具窗口将会以树状形式呈现元素,如字段,方法和内部类。当编辑器显示诸如activity_main.xml的XML文件时,Structure工具窗口以树状结构呈示XML元素。在Structure工具窗口中点击任何元素,光标将会立即移动到编辑器中的该元素上。Structure工具窗口特别适合导航超大源文件中的元素。通过打开Structure工具窗口和导航MainActivity.java与activity_main.xml中的元素来练习此技巧。如图2-4所示。


Figure 2-4. The Structure tool window
<翻译>  图2-4 结构工具窗口

The Favorites Tool Window

收藏夹工具窗口

When developing a feature (or debugging a bug) in Android, you will likely create or modify several related files. Moderately complex Android projects may contain hundreds of individual files, so the ability to group related files is useful indeed. The Favorites tool window contains favorites that allow you to logically group references to related files that might otherwise be physically located in completely different parts of your project.

<翻译>当在Android中开发一个功能(或者调试bug)的时候,你可能会创建或修改一些相关文件。中等复杂的Android项目可能包含数百单个文件,因此,具有分组相关文件的能力是非常有用的。Favorites工具窗口能够容纳那些你将 实际上位于你项目中完全不同的地方的相关文件进行逻辑分组的最爱。

Make sure that both the MainActivity.java and activity_main.xml files are loaded as tabs in the Editor. Now right-click (Ctrl-click on Mac) either tab in the Editor and select Add All to Favorites from the context menu. In the Input New Favorites list Name field, type main and press OK. If the Favorites tool window is not open, activate it now by toggling Alt+2 | Cmd+2. Expand the favorites item called main, and double-click one of the files listed therein to open/activate it.

<翻译>将MainActivity.java和activity_main.xml文件在编辑器选项卡中打开。在任意一个选项卡上右击(Mac下按住Ctrl单击),在上下文菜单中选择Add All to Favorites。在Input new favorites list name中输入main然后点击OK。如果Favorites工具窗口没有打开,通过Alt+2|Cmd+2来打开。展开列表中的main,然后通过双击来打开/激活其中的一个文件。

Just as the Favorites window allows you to navigate immediately to any particular file or groups of files, bookmarks allow you to navigate immediately to any particular line in a file. Position your cursor on any line in MainActivity.java. Now press F11 (F3 on Mac). This action creates or removes a bookmark in any source file, including XML files. Notice both the checkmark in the gutter and the black tick in the marker bar indicating the new bookmark. To view the bookmark you just created, toggle open the bookmarks in the Favorites tool window.

<翻译>就像Favorites窗口能够让你立即导航到特定文件或文件组一样,书签能够让你快速导航到文件的特定行。将你光标放到MainActivity.java的任意一行。按一下F11(Mac下按F3)。这个操作便是创建或者删除资源文件包括XML文件中的书签。观察边列的检查标志和标记栏的黑色标记来指示新的标签。通过打开Favorites工具窗口中的书签来查看你刚刚创建的书签。

Note On a PC, if F11 does not seem to be responding, check to make sure that the F-Lock key is
activated on your keyboard.

<翻译>  注意: 在PC上,如果F11没有响应,请确保你键盘上的F-Lock键是激活的。

<译者注:Android Studio一直在升级,此处F11没有效果的童鞋可以通过查看点击菜单栏的Navigate ➤Bookmarks来查看标签快捷键。>

Breakpoints are used for debugging. Unlike bookmarks, which may be set in any file, you need to be in a Java source file in order to set a breakpoint. Open MainActivity.java and click in the gutter next to the following line of code:

<翻译>断点是用来调试的。不像标签,可以在任何文件中进行设置,断点只能在java文件中进行设置。打开MainActivity.java,然后在如下代码行的边列上单击:

setContentView(R.layout.activity_main);


You will notice that a red circle now occupies the gutter and that the line is also highlighted red. Breakpoints can be set only on executable lines of code; trying to set a breakpoint on a comment line, for example, will not work. To view your newly created breakpoint, toggle open the Breakpoints tree in the Favorites tool window. There are several more interesting things you can do with breakpoints, and we discuss breakpoints at length in Chapter 12, which is devoted to debugging.

<翻译>你会发现一个红色圆圈出现在边列,并且整行代码都是红色高亮的。断点只能被设置在可执行的代码行上;举个例子,如果在注释行上设置断点,并没有什么效果。打开Favorites工具窗口中的Breakpoints查看你刚刚创建的断点。你可以用断点做很多有趣的事,在之后的第12章,我们将讨论用于调试的断点。

The TODO Tool Window

TODO工具窗口

TODO means, of course, to do. TODOs are essentially comments that indicate to the programmers and their collaborators that there remains work yet to be done. TODOs are written like comments, beginning with two forward slashes, the word TODO in all-caps, and a space. For example:

<翻译> TODO的意思当然是要做的事。TODO本质上是用于程序员们和他们的合作者表明还要做的事。TODO写法跟注释类似,以双斜杠开头,然后TODO大写,后面跟一个空格。例如:

//TODO inflate the layout here.


Create a TODO in MainActivity.java and open the TODO tool window to view it. Clicking a TODO in the TODO tool window immediately jumps to that TODO in your source code.

<翻译>在MainActivity.java中创建一个TODO然后打开TODO工具窗口,就可以看到它。在TODO工具窗口中点击一个TODO,将会立即跳转到源代码中的TODO。

The Commander Tool Window

Commander工具窗口

The Commander tool window is a navigation aid with left and right panes. These panes function much like the Project and Structure tool windows do. The Commander tool window differs from these other navigation windows in that it displays only one directory level at a time, rather than displaying nested directory trees. If you prefer Windows-style navigation or you find that the Project tool window is too overwhelming, then the Commander tool window may be a good navigation alternative.

<翻译> Commander工具窗口是左右窗格的导航。这些窗格的功能类似Project和Structure工具窗口。Commander窗口和其他导航窗口的不同之处在于,它在一个时间段内只显示一个目录层次,而不是显示目录树。如果你很喜欢窗口样式的导航或者你觉得Project工具窗口太繁琐,那么,Commander工具窗口可能是一个很好的导航选择。

<译者注:本书由于出版比较早,所以Android Studio某些功能已经经过大改,比如Commander已经在新版本中消失,所以,各位童鞋可以酌情参考。>
转自:www.android-studio.org
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值