[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

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


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.


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:



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.


The TODO Tool Window


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.


The Commander Tool Window


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已经在新版本中消失,所以,各位童鞋可以酌情参考。>
2015-12-21 15:52 添加评论 分享




whappyjpeng - 孤独的享受



SadieYu - 爱生活,爱狗狗,IT民工助理,码农秘书。



AsCoder - Duang!风口来了,猪都飞了!你还不飞吗?



xuanwen - 90后大兄弟



hdgrp - 对新的事物感兴趣,但总是不能持久。加油





Android Studio


Dream - 不一样的程序员





thanks a lot

sally - 90后



fistsunly - 认证负责



fatboy - 虽然我胖,但我吃得多啊



shen - it




退出全屏模式 全屏模式 回复