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

The Main Menu Bar

主菜单栏

The main menu bar is the uppermost bar in Android Studio, and you can execute virtually any action by navigating through its menus and submenus. Unlike the other bars in Android Studio, the main menu bar cannot be hidden. Don’t be overwhelmed by the many actions contained in the main menu bar and its submenus. Even the most seasoned Android developer will use only a fraction of these actions on a daily basis, and most of the actions have corresponding keyboard shortcuts and/or context menu items. We discuss many of the actions contained in the main menu bar in subsequent sections and chapters.

<翻译> 主菜单栏位于Android Studio的最上面,你几乎可以利用主菜单和其子菜单来执行任何操作。不像Android Studio中其他的一些菜单,主菜单不能被隐藏。不要被主菜单和它的子菜单项吓到。即使经验丰富的Android开发者也在日常编码中仅仅使用其中的一小部分,更多的则是通过快捷键和相应的上下文菜单实现。我们将在后面的章节中讨论主菜单栏中大部分的操作。

The Toolbar

工具栏

The toolbar contains buttons for frequently used text operations such as Cut, Copy, Paste, Undo and Redo. As you’ve already seen in Chapter 1, the toolbar also contains buttons to various managers within Android Studio, including the SDK Manager and the Android Virtual Device Manager. The toolbar also has buttons for Settings and Help, as well as buttons to Run and Debug your app. All of the buttons in the toolbar have corresponding menu items and keyboard shortcuts. Advanced users may want to hide the toolbar to conserve screen real-estate by unchecking the View ➤ Toolbar menu item.

<翻译> 工具栏中包含一些频繁使用的文本操作按钮,例如剪切、复制、粘贴、撤销、重做。正如你在第一章看到的,工具栏中还包含各种各样的管理器,包括SDK管理器和Android虚拟设备管理器。工具栏中还有设置和帮助按钮以及运行和调试应用程序按钮。工具栏中所有的按钮都有相应的菜单项和快捷键。高级用户可以通过取消勾选View下的Toolbar 菜单项来释放屏幕空间。

The Navigation Bar

导航栏

The navigation bar displays a horizontal chain of arrow boxes representing the path from your project’s root directory (on the left) to the currently selected tab in the Editor (on the right). The navigation bar may be used to navigate your project’s assets without having to resort to the Project or Commander tool windows.

<翻译> 导航栏是以水平箭头的链状结构方式来显示从项目根目录(左边)依次到编辑器(右边)中选中的选项卡。导航栏可以用来导航你项目中的资源文件而不必通过Project或者Commander工具窗口。

The Status Bar

状态栏

The status bar, shown in Figure 2-5 (and previously in Figure 2-1), displays relevant and context-sensitive feedback, such as information about any running processes or the state of your project’s Git repository. Let’s explore the status bar in some detail now.

<翻译> 如图2-5所示(以前在图2-1),状态栏显示一些相关的和上下文敏感反馈信息,比如正在运行中的进程或者你项目中Git版本库状态的信息。现在我们将详细的讨论一下状态栏。

Figure 2-5. Status bar
图2-5 状态栏


In the leftmost corner of the status bar is the Toggle Margins button. Clicking this button toggles hiding and showing the margins. In addition, when you hover your mouse over this button, a context menu appears that allows you to activate any of the tool windows.

<翻译> 在最左边角落里的是边栏切换按钮。单击此按钮就可以隐藏或者显示边栏。另外,当你的鼠标悬停在上面的时候,会出现一个上下文菜单以便你激活任意一个工具窗口。

The message area is used to provide feedback and display any information about concurrently running processes. This area also displays hints as you roll your mouse over UI elements such as menu items or buttons in the toolbar. Clicking on this area opens the Event log.

<翻译> 消息区域用来提供反馈信息,同时显示运行过程中的所有信息。当你在UI上比如菜单项或者工具栏的按钮上滚动鼠标的时候,这块区域会显示相应的提示。点击这块区域打开事件日志。

The Editor cursor position displays the location of your cursor in the Editor in line:column format. Clicking on this area activates a dialog box allowing you to navigate directly to a particular line in your code.

<翻译> 编辑器光标位置区域以行:列的形式显示编辑器中光标的位置。点击这块区域激活对话框来直接导航到你代码中特定的行。

The line separator area displays the format of the carriage returns used in your text files. On Windows, the default is CRLF, which stands for carriage return line feed. LF is the standard format used on Unix and Mac machines, as well as in Git. If you’re developing on a Windows computer, Git will typically convert from CRLF to LF when committing your code to the repository.

<翻译> 行间隔符区域用来显示你文本文件中的回车键格式。在Windows下默认使用CRLF代表回车换行。如果是Unix和Mac机器的话则使用LF,Git中一样。如果你在Windows电脑上开发的话,Git通常会在你提交代码的时候自动将CRLF改为LF。

The text format area describes the text encoding used for your source files. The default is UTF-8, which is a superset of ASCII and includes most of the Western alphabets, including any characters that you might find in a standard Java or XML file.

<翻译> 文本格式区域用来源文件中的文本编码格式。默认是UTF-8,是ASCII的超集,它包含了大部分的西文字母和标准Java或XML文件中的所有字符。

The file access indicator area allows you to toggle between read/write and read-only. An unlocked icon means that the current file in the Editor has read/write access. A lock icon means that the current file in the Editor is read-only. You can toggle these settings by clicking the indicator’s icon.

<翻译> 文件访问指示器区域能够在读/写和只读之间切换。解锁图标意味着在当前编辑器中具有读/写功能。锁定图标意味着编辑器中的文件是只读的。你可以通过点击指示图标来切换设置。

The Highlighting Level button activates a dialog box with a slider that allows you to set the level of highlighting you want to see in your code.

<翻译> 点击高亮等级按钮,激活一个带滑块按钮的对话框,在这你可以设置代码中高亮显示的等级。

The default setting is Inspections, which corresponds to an icon of a frowning Inspections Manager. This setting indicates that you should be prepared for some tough love, as the Inspections Manager will be strict in identifying both syntax errors and possible problems with your code, called warnings. You can see some of the warnings generated by the Inspections Manager in the marker bar as yellow ticks.

<翻译> 默认设置是Inspections,对应一个皱着眉头的审查经理图标。此设置为了帮助你,审查经理将对你代码中的语法错误和被称作警告的可能出现的问题进行严格的检查。你可以在标记栏上看到审查经理生成的黄色警告标识。

The next setting on the slider is Syntax, which corresponds to an icon of the Inspections Manager in profile. For this setting, the Inspections Manager is turning a blind eye to warnings. Syntax mode is less strict than Inspections mode, but still highlights problems with syntax that will prevent your code from compiling.

<翻译> 滑动块的下一个设置是Syntax,对应一个侧着脸的审查经理的图标。这个设置下,审查经理是睁一只眼闭一只眼的。Syntax检查不如Inspections严格,但是仍然会高亮显示阻止代码编译的语法问题。

The last highlight mode on the slider is None, which corresponds to an icon of a smiling Inspections Manager. This icon makes me think that the Inspections Manager is happy-drunk and just doesn’t care about your code. Even the most egregious syntax errors are ignored in this mode, though the compiler will still choke on them when you attempt to build. I recommend leaving the highlight level to Inspections and learning to appreciate the Inspections Manager’s tough love.

<翻译 最后一个高亮模式是None,对应一个微笑着的审查经理图标。这个图标让我想到审查经理很开心并且不关心你的代码。这种模式下,即使是最严重的语法错误也会被忽略,尽管当你试图构建的时候编译器仍然会阻止。我建议将高亮等级设置为Inspections并且学会欣赏审查经理的“严厉的爱”。

Common Operations

常用操作

This section reviews various common operations used in Android Studio. If you’ve used a text editor like Microsoft Word, you will likely be familiar with the features covered in this section.

<翻译> 本节介绍在Android Studio中的各种常用操作。如果你使用过像微软的Word类似的文本编辑器的话 ,那么你可能对这节比较熟悉。

Selecting Text

<选择文本>

As you would expect from any good text editor, double-clicking any word in a source file selects it. In addition, clicking and dragging the cursor across letters or words selects those text elements. Placing your cursor anywhere in a source file and pressing Shift+Down-Arrow or Shift+Up-Arrow selects lines of text beginning at the cursor. Triple-clicking anywhere on a line of text selects the entire line. Pressing Ctrl+A | Cmd+A selects all text in a file.

<翻译> 正如你想的那样,任何一个好的文本编辑器,在源文件中双击任何单词就能够选中它。此外,单击并拖动光标选择单词或者字母中的文本元素。将光标放在源文件中,并按下Shift+下箭头或者Shift+上箭头来选择从光标处开始的行文本。在行文本的任何地方三击来选择整行。按下Ctrl+A|Cmd+A选择文件中的所有文本。

If you place your cursor inside any word and press Ctrl+W | Alt+Up-Arrow, the entire word becomes selected. If you continue to press Ctrl+W | Alt+Up-Arrow, the selection grows to include adjacent text ad infinitum. If you now press Ctrl+Shift+W | Alt+Down-Arrow, the selection shrinks. This growing/shrinking selection functionality is called structural selection in Android Studio.

<翻译> 如果你将光标放置在任何单词上,并按下Ctrl+W|Alt+上箭头将会选中整个单词。如果你继续按下Ctrl+W|Alt+上箭头,将会无止境的选择相邻的文本。如果此时你按下Ctrl+Shift+W|Alt+下箭头,文本选择将会收缩。这种增加/减少选择的功能在Android Studio中被称为结构化选择。

Using Undo and Redo

使用撤销和重做

The Undo and Redo commands are useful for rolling back and rolling forward a limited number of edit operations. Changes are delimited by specific UI events such as pressing Enter or repositioning the cursor. The keyboard shortcuts for Undo and Redo are Ctrl+Z | Cmd+Z and Ctrl+Shift+Z | Cmd+Shift+Z, respectively. There are purple right- and leftarrows on the left side of the toolbar that will do the same. The default on Android Studio is to remember all your steps back to your last save or up to 300 steps. Undo and Redo are applied to only one file at a time, so the most effective way to roll back changes is to use Git, which is discussed in Chapter 7.

<翻译> 撤销和重做命令对于回滚有限数量的编辑操作是非常有用的。特殊的UI事件比如按下Enter或者重新定位鼠标用来限改变。撤销和重做的快捷键分别是Ctrl+Z|Cmd+Z和Ctrl+Shift+Z|Cmd+Shift+Z。工具栏左边的紫色左右箭头也分别代表撤销和重做。Android Studio会记住你上次保存至今的所有步骤或者达到上限300步。撤销和重做只能在一段时间内应用到一个文件,因此,最有效方式来回滚更改是利用Git,将在第7章中讨论。

Finding Recent Files

查找最近文件

Among the best features of Android Studio is that it remembers all the files you worked on recently. To activate this command, choose View ➤ Recent Files or press Ctrl+E | Cmd+E. The resulting dialog box allows you to select any recent file and opens it as a tab in the Editor. The default limit remembers up to 50 previous files. You can change these limits by navigating to File ➤ Settings ➤ Limits ➤ Editor ➤ Recent Files Limit.

<翻译> Android Studio中最好的特性是它记住了你最近是用过的所有文件。选择View>Recent Files或者按下Ctrl+E|Cmd+E来激活这个命令。结果对话框允许你选择任何最近的文件,并将之作为编辑器中的一个选项卡来打开。默认只能记住之前的50个文件。你可以通过File ➤Settings ➤Limits ➤Editor ➤Recent Files Limit来修改这些限制。

Traversing Recent Navigation Operations

遍历最近导航操作

Android Studio also remembers your recent navigation operations. Navigation operations include cursor moves, tab changes, and file activations. To traverse your navigation operations history, press Ctrl+Alt+Left-Arrow | Cmd+Alt+Left-Arrow or Ctrl+Alt+Right-Arrow | Cmd+Alt+Right-Arrow. Keep in mind that navigation operations are different from edit operations; if you want to traverse your edit operations, you should use Undo and Redo.

<翻译> Android Studio也能够记住你最近的导航操作。导航操作包括指针移动、标签变更和文件激活。按下Ctrl+Alt+左箭头|Cmd+Alt+左箭头或者Ctrl+Alt+右箭头|Cmd+Alt+右箭头来遍历你的导航操作历史。注意,导航操作不同于编辑操作;如果你想遍历你的编辑操作,你应该使用撤销和重做。

Cutting, Copying, and Pasting

剪贴,复制,粘贴

If you’ve used any text editor or word processor, you’re familiar with Cut, Copy, and Paste. Table 2-2 lists these basic commands, as well as some of the extended clipboard commands.

<翻译> 如果你使用过任何的文本编辑器或者文档处理器,你应该熟悉剪切、复制和粘贴。表2-2列出了这些基本命令以及一些扩展的剪贴板命令。

Table 2-2. Cut, Copy, and Paste
表2-2 剪贴,复制,粘贴


In addition to the simple Cut, Copy, and Paste functionality provided by the OS clipboard, Android Studio has an extended clipboard that remembers the last five Cut and Copy operations. When you cut or copy text from Android Studio—or virtually any other application while Android Studio is running—Android Studio places that text onto a stack. To see the extended clipboard stack, press Ctrl+Shift+V | Cmd+Shift+V. The resulting dialog box allows you to choose whichever item you’d like to paste. See Figure 2-6.

<翻译> 除了协同剪贴板提供的一些简单的剪切、复制和粘贴操作,Android Studio扩展了剪贴板的功能,能够记住之前五步剪贴和粘贴操作。在Android Studio运行的时候,当你从Android Studio剪切或者复制文本(或者任何其他的应用程序)的时候,Android Studio将会将这些文本放到一个堆栈中。按下Ctrl+Shift+V|Cmd+Shift+V就能够看到扩展的剪贴板堆栈。结果对话框允许你选择粘贴其中一项。如图2-6所示。


Figure 2-6. Extended clipboard
图2-6 展开剪贴板


You can also change the size of the extended clipboard stack by navigating to File ➤ Settings ➤ Limits ➤ Editor ➤ Maximum Number of Contents to Keep in Clipboard. You can also compare any currently selected text with that of the most recent element in the extended clipboard by right-clicking the selection and selecting the Compare with Clipboard menu item.

<翻译> 你也可以通过导航栏上的File ➤Setting ➤Limits ➤Editor ➤Maximum Number of Contents来更改扩展剪贴板上内容的数量。你也可以通过在选中的文本上右击,然后选择Compare with Clipboard项来将当前选中的文本和剪贴板中最近的元素来进行比较。

The Copy Path command Ctrl+Shift+C | Cmd+Shift+C copies the fully qualified operating system path of any file or directory selected in the Project or Commander tool windows, or any tab of the Editor. Copy Path is particularly useful for operations in a terminal session.

<翻译> 复制路径命令Ctrl+Shift+C|Cmd+Shift+C用来复制任何文件的物理路径或者Project或Commander工具窗口中文件夹的路径或者编辑器中选项卡的路径。在终端会话中复制路径操作是非常有用的。

With Copy Reference Ctrl+Alt+Shift+C | Cmd+Alt+Shift+C, Android Studio allows you to copy a logical reference to a method, variable, or class. When you paste this reference into another source file, Android Studio automatically includes any required package qualifiers and imports. You can also use generic Cut, Copy, and Paste on packages, directories, and files in the Project and Commander tool windows in lieu of mouse operations such as drag-and-drop in order to reorganize the location of assets in your project.

<翻译> 通过Ctrl+Alt+Shift+C|Cmd+Alt+Shift+C命令,Android Studio能够复制一个逻辑引用到方法、变量或类中。当你将这个引用复制到另一个源文件的时候,Android Studio会自动的包含所需的所有修饰符和imports。你也可以在Commander和Project工具窗口中的包、目录和文件中使用剪切、复制和粘贴来代替鼠标直接拖拽来整理你项目中的资源文件。

Context Menus

上下文菜单

Numerous context menus can be activated by right-clicking (Ctrl-clicking on Mac) on the IDE. You’ve already explored the Editor tab context menu in a previous section. Most panes, icons, and bars in Android Studio will generate a context menu if you right-click (Ctrl-click on Mac) it. One of the greatest features of Android Studio is that actions may be performed in more than one way. This redundancy means that you are free to develop your skills and habits according to your own preferences. I find that using keyboard shortcuts for the most frequent operations, and menu and context-menu actions for less-frequent operations is the most effective way to interface with Android Studio. Explore the context menus by rightclicking (Ctrl-clicking on Mac) bars, tabs, panes, and files in the IDE now.

<翻译> 大多数的上下文菜单都可以在IDE中通过右击来激活(mac下按住ctrl单击)。在前面的章节,你已经学习了编辑器选项卡的上下文菜单。Android Studio中的大多数的窗格,图标和边栏,如果你右击(mac下按住ctrl单击)将会出现一个上下文菜单。Android Studio的最大优势是执行一个操作仅仅一种方式。这种冗余意味着你可以根据你自己的喜好习惯来免费扩展你的技能和习惯。我发现一些频繁的操作使用快捷键,而对一些低频率的操作使用菜单和上下文菜单进行操作是非常有效的方法来使用Android Studio。接下来通过右击(mac下按住ctrl单击)IDE的边栏,选项卡,窗格和文件来探索上下文菜单。

Getting Help

获取帮助

The Help menu in Android Studio has several useful menu items. Find Action (Ctrl+Shift+A | Cmd+Shift+A) is the command you will use most often to get help in Android Studio. This command activates a dialog box that allows you to search for any feature in Android Studio. Press Ctrl+Shift+A | Cmd+Shift+A and type Show Line Numbers in the search box. Now use your arrow keys to select Settings and press Enter. In the Settings window, choose Editor ➤ Appearance. You should see the Show Line Numbers check box.

<翻译> Android Studio中的帮助菜单有几个有用的菜单项。查找操作(Ctrl+Shift+A|Cmd+Shift+A)是Android Studio中用的最多的获得帮助的命令。这个命令行会激活一个对话框,允许你搜索Android Studio中的任何东西。按下Ctrl+Shift+A|Cmd+Shift+A并且在搜索框中输入Show Line Numbers,然后使用鼠标选择Setting然后按下Enter。在设置窗口,选择Editor ➤ Appearance。(译者注:Android Studio是Editor》General》Appearance)。接着你就能看到Show Line Numbers的复选框了。

Choosing Help ➤ Online Documentation is your source to all the technical specifications forAndroid Studio. This is the most comprehensive documentation for Android Studio. Also, the Help ➤ Default Keymap Reference menu item is a useful reference. You may consider printing this PDF and keeping it nearby as you learn to use Android Studio.

<翻译> 选择Help➤Online Documentation,你能够看到Android Studio中的所有技术规范文档资源。这是Android Studio最全面的文档。同时,Help➤Default Keymap Reference菜单项也是一个非常有用的参考。你可以考虑将PDF文档打印出来,然后在你学习使用Android Studio的时候放在手边时时参考。

Navigating with the Keyboard

导航快捷键

The keyboard is perhaps the most powerful way to navigate around Android Studio. Select the Navigate menu from the main menu bar to inspect its contents. This section discusses the most important menu items (shown in Table 2-3) and their corresponding keyboard shortcuts from the Navigate menu. Subsequent chapters discuss other menu items.

<翻译> 快捷键是最强大的方式来浏览Android Studio。在主菜单栏选择Navigate菜单浏览其内容。本节将讨论一些重要的菜单项(如表2-3)及其对应的导航菜单快捷键。随后的章节讨论其他的菜单项。

Table 2-3. Keyboard Navigation


表2-3 导航快捷键

Select In

选择在

One of the best features of Android Studio is that navigation is bilateral. You’ve already seen how to open/activate files as tabs of the Editor from various tool windows. Now you’re going to learn how to navigate to various tool windows from the Editor.

<翻译> Android Studio最好的特性之一是导航是双边的。你已经见识过如何在各种工具窗口中作为编辑器的选项卡来打开/激活文件。现在你将要学习如何从编辑器中导航到 各种工具窗口。

Press Alt+F1. This activates the Select In context menu, which contains several menu items, including Project View, Favorites, and File Structure. Click the Project View option. The Project tool window becomes activated, the file corresponding to the active tab of the Editor is highlighted, and any parent directories of that file are toggled open. Android projects tend to have a lot of file assets; therefore, using Select In is among the most important skills that you will master.

<翻译> 按下Alt+F1。激活选择上下文菜单,其中包含几个菜单项,包括项目视图,收藏和文件结构等。点击项目视图操作。项目工具窗口将会激活,文件对应的编辑器中活动的选项卡将会高亮显示,并且该文件的父目录将会自动打开。Android项目中有很多的资源文件;因此,会用选择操作是最重要的熟练技能之一。

Class

The Class action allows you to navigate to a particular Java class. It’s important to note that this action searches for only Java source files, or inner classes of Java source files. Press Ctrl+N | Cmd+O and start typing act. Android Studio has already indexed all of your files, and so it will provide you a list of possible matches, with the most likely match highlighted. All you need to do is press Enter to open MainActivity.java.

<翻译> 类操作允许你导航到一个特定的java类。值得重点关注的是这个操作只能查找Java资源文件或是Java资源文件的内部类。按下Ctrl+N|Cmd+O然后开始输入act。Android Studio将检索你所有的文件,并且提供可能匹配的列表,将最可能匹配的高亮显示。你只需要按下Enter来打开MainActivity.java。

File

文件

The File action allows you to navigate to any file in your project. If you’re looking for an XML file in your project, this is the action that you will want to use. Press Ctrl+Shift+N | Cmd+Shift+O and start typing act. We’ve used the same search term act on purpose to illustrate the wider scope of Navigate ➤ File. Notice that the search results include the Java source file MainActivity.java as well as any other files, such as activity_main.xml. Use the arrow keys to select activity_main.xml and press Enter to open it.

<翻译> 文件操作允许你导航到项目中的任何文件。如果你想查找你项目中的xml文件,那么你将用这个操作。按下Ctrl+Shift+N|Cmd+Shift+O然后输入act。我们故意使用了相同的检索项来说明Navigate》File中的更广泛的范围。可以注意到搜索结果中包含Java资源文件MainActivity.java和一些其他的文件,如activity_main.xml。用 鼠标来选择activity_main.xml然后按下Enter键来打开。

Line

The Line action Ctrl+G | Cmd+L activates a dialog box that allows you to navigate to a particular line:column of your source file. If you type a simple integer in the resulting Go to Line dialog box and press OK, Android Studio will jump to that line without regard to column.

<翻译> 使用Ctrl+G|Cmd+L来执行行操作,会激活一个对话窗口,然后以Line:Column形式导航到你的资源文件。如果你在Go to Line对话框中输入一个简单的整数然后点击OK,Android Studio将会跳转到指定的行而不管列。

Related File

相关文件

The Related File action Ctrl+Alt+Home | Alt+Cmd+Up-Arrow is one of the most useful commands in Android Studio. Android projects typically have a lot of related files. For example, a simple Android activity usually has at least one corresponding XML layout file that renders the activity’s layout, and one corresponding XML menu file that renders the activity’s menu. As you work with fragments, this complexity only increases. You’ve already
seen how to group related files together by using Favorites. With Navigate ➤ Related File, you can query Android Studio to show you related files. With the MainActivity.java tab activated, press Ctrl+Alt+Home | Alt+Cmd+Up-Arrow. You should see activity_main.xml listed there. Use your arrow keys to select it and press Enter.

<翻译> 按下Ctrl+Alt+Home|Alt+Cmd+上箭头来打开相关文件夹操作是Android Studio中一个非常有用的命令行。Android项目通常有一个非常多的相关文件。例如,一个简单的Android Activity通常有至少一个相对应的xml布局文件来呈现Activity的布局,和一个相对应的XML菜单文件来呈现Activity的菜单。如果你使用Fragments,这只会增加复杂性。你已经知道通过Favorites如何组织相关文件。Navigate ➤Related File中,你可以看到Android Studio中展示给你的相关文件。打开MainActivity.java选项卡,按下Ctrl+Alt+Home|Alt+Cmd+上箭头。你应该能够看到activity_main.xml列出来了。使用鼠标来选择然后按下Enter。

Last Edit Location

最后编辑位置

The Last Edit Location action Ctrl+Shift+Backspace | Cmd+Shift+Backspace allows you to navigate to your last edit. If you continue to activate this command, your cursor will move to the file/location of your previous edit, and so on.

<翻译> 按下Ctrl+Shift+Backspace|Cmd+Shift+Backspace激活最后编辑位置,这个位置允许你导航到最后的编辑位置。如果你继续激活这个命令,你的鼠标将会移动到之前编辑的文件/位置,等等。

Type Hierarchy

类型层级结构

Android uses Java, an object-oriented programming language. One of the hallmarks of any object-oriented language is inheritance, which facilitates code reuse and polymorphism. With the MainActivity.java file active in the Editor, press Ctrl+H to toggle open the Hierarchy tool window. There you will see a cascading series of objects, all of which can trace their ancestry to the progenitor of all objects in Java called Object. Keep in mind that the Navigate ➤ Type Hierarchy action will work only when the active tab in the Editor is a Java source file.

<翻译> Android使用的是面向对象的编程语言Java。面向对象语言的最大特点之一是继承,它促进了代码的重用和多态性。在编辑器中打开MainActivity.java文件,按下Ctrl+H来切换打开层次结构窗口。在打开的窗口你将看到一系列的层叠对象,所有的对象都可以追溯到祖先的在Java中被称作对象的所有对象。需要记住的是只有在编辑器中打开的是Java资源文件的话Navigate ➤Type Hierarchy才可以激活。

Declaration

声明

The Declaration action allows you to jump to the original definition of methods, variables, and resources. Another way to activate this action is by holding the Ctrl|Cmd key down while rolling your mouse over methods, variables, or resources in your file. If the element becomes underlined, you may navigate to its declaration by left-clicking the element while continuing to hold down the Ctrl|Cmd key. In MainActivity.java, click your cursor anywhere in the method setContentView(...) and press Ctrl+B | Cmd+B. You will be taken immediately to this method’s declaration, which is located in one of MainActivity’s superclasses called ActionBarActivity.java.

<翻译> 声明操作允许你跳转到原始的定义方法、变量和资源。激活这个操作的另一种方式是按住Ctrl|Cmd然后在文件中的方法,变量或资源文件上滚动鼠标。如果这个元素上有下划线,你可以通过单击导航到它的声明上。在MainActivity.java中,点击方法setContentView中的任何地方然后按下Ctrl+B|Cmd+B。你将会立即跳转到该方法的声明,它位于MainActivity的超类ActionBarActivity.java中。

Finding and Replacing Text

查找和替换

Finding and replacing text is an essential part of programming, and Android Studio has a powerful suite of tools to help you do just that. This section covers some of the most important tools. Table 2-4 lists them for you.

<翻译> 查找和替换文本是编程的一个重要部分,Android Studio有一套强大的工具来帮助你做到这一点。本节讨论一些最重要的工具。如表2-4所列。

Table 2-4. Find and Replace


表2-4 查找和替换

Find

查找

The Find action is used to find text occurrences within a single file. In MainActivity.java, press Ctrl+F | Cmd+F to bring up a search bar that appears along the top of the Editor. Type action in the search box of the search bar. You will notice that action is immediately highlighted in yellow throughout your file. You will also notice small green ticks in the marker bar indicating the locations of the found text. Rolling your mouse over the double rightarrows on the find bar will display advanced search options.

<翻译> 查找操作用于查找在本文件中的文本。在MainActivity.java中,按下Ctrl+F|Cmd+F在编辑器的最上方弹出一个搜索栏。在搜索栏中输入action,你将注意到文件中会立即以黄色高亮显示。你也可以在标记栏上看到一些绿色的标记用来标记查找到的文本的位置。将鼠标移到查找栏的双右箭头上将会显示高级搜索选项。

Find in Path

路径中查找

The Find in Path action allows you to search in a much wider scope than with the Find action described previously. You can also use regular expressions, and delimit results with a file mask. Press Ctrl+Shift+F | Cmd+Shift+F and type hello in the search box of the search bar along the top of the Editor. By default, the search scope in Find in Path is set to Whole Project, though you can limit the search scope to a particular directory or module. Accept the default of Whole Project and click the Find button. The results appear in the Find tool window. Clicking an entry in the Find tool window immediately opens the enclosing file as a new tab of the Editor and jumps to that occurrence.

<翻译> 查找路径操作比之前讲的查找操作查找的范围更广泛的范围。你也可以使用正则表达式和界定文件覆盖的结果。按下Ctrl+Shift+F|Cmd+Shift+F并且在搜索框中输入hello。默认情况下,搜索范围被设置为整个项目,但是你可以限制搜索范围到一个特殊的目录或是module。同意整个项目的默认设置并且点击查找按钮。结果将会显示在查找工具窗口。点击查找窗口中的一个条目将会立即将关闭的文件打开为编辑器中一个新的选项卡并且跳转到搜索的地方上。

Replace

替换

The Replace action Ctrl+R | Cmd+R is used to replace a text occurrence in a single file, and the functionality of Replace is a superset of Find. The safer way to replace text is to use the Refactor ➤ Rename command, which we will cover later.

<翻译> 通过Ctrl+R|Cmd+R打开替换操作,这个操作用于替换当前文件中的文本,并且替换功能是查找的超集。替换文本最安全的方法就是使用Refactor》Rename命令,稍后我们将介绍它。

Replace in Path

路径中替换

The Replace in Path action Ctrl+Shift+R | Cmd+Shift+R is a superset of Find in Path. However, it’s almost always better to use Refactor ➤ Rename than to use Replace in Path, so use this command with extreme caution as you could introduce errors.

<翻译> 通过Ctrl+Shift+R|Cmd+Shift+R打开在路径中替换操作,这个操作是在路径中查找的超集。然而,使用Refator➤Rename会比在路径中替换更好,因此,当你引入错误的时候你将及其谨慎的使用这个命令。

Summary

总结

In this chapter, we’ve discussed the Editor and the tool windows that cluster around the Editor. We’ve discussed how to use the tool buttons and reposition them. We’ve also discussed those tool windows that are used for navigation and the major UI elements of the IDE, including the main menu bar, the toolbar, the status bar, the gutter, and the marker bar. We’ve also discussed how to search and navigate by using menus and keyboard shortcuts, as well as using Find and Replace. Finally, we discussed how to use the help system in Android Studio. Most important, we’ve established a lexicon of UI elements in Android Studio to which we will refer in subsequent chapters.

<翻译> 在本章中,我们讨论了编辑器和编辑器周围的工具窗口。我们讨论了如何使用工具按钮和将他们重定位。我们也讨论了包括主菜单栏,工具栏,状态栏,边栏和标记栏的用于导航的工具窗口和主要的UI元素。我们也讨论了如何去搜索和通过使用菜单栏和快捷键来导航,以及使用查找和替换。最后,我们讨论了Android Studio中怎样使用帮助系统。更重要的是,我们已经在Android Studio中建立了一个UI元素,这些我们将在后续章节中引用。
2015-12-27 16:18 添加评论 分享
已邀请:
1

Dream - 不一样的程序员

赞同来自: Aug

文档写的很详细,谢谢
0

ask

赞同来自:

非常感谢!
0

fwt336

赞同来自:

非常感谢
0

xuanwen - 90后大兄弟

赞同来自:

很细致
0

uzili

赞同来自:

感谢分享
0

kclme

赞同来自:

thanks a lot
0

hanguang - 80后很难

赞同来自:

感谢分享
0

xy96340 - 软件开发者

赞同来自:

非常感谢!要好好学习
0

sdzwd

赞同来自:

学习了,非常感谢分享

要回复问题请先登录注册

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