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


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.

Current Layout as Default(将当前布局存储为默认布局)和Restore Default Layout(恢复默认布局)。恢复默认布局通常用在IDE窗口变得拥挤,或者你想清理面板将之还原到你熟悉的布局。你也可用通过打开,关闭,调整或者重新定位你喜欢的窗口,然后选择将当前布局存储为默认布局来设置新的默认布局。


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.


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.


Table 2-1. Navigation Tool Windows