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的最上面,你幾乎可以利用主菜單和其子菜單來執(zhí)行任何操作。不像Android Studio中其他的一些菜單,主菜單不能被隱藏。不要被主菜單和它的子菜單項嚇到。即使經(jīng)驗豐富的Android開發(fā)者也在日常編碼中僅僅使用其中的一小部分,更多的則是通過快捷鍵和相應(yīng)的上下文菜單實現(xiàn)。我們將在后面的章節(jié)中討論主菜單欄中大部分的操作。
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.
<翻譯>翻譯> 工具欄中包含一些頻繁使用的文本操作按鈕,例如剪切、復(fù)制、粘貼、撤銷、重做。正如你在第一章看到的,工具欄中還包含各種各樣的管理器,包括SDK管理器和Android虛擬設(shè)備管理器。工具欄中還有設(shè)置和幫助按鈕以及運行和調(diào)試應(yīng)用程序按鈕。工具欄中所有的按鈕都有相應(yīng)的菜單項和快捷鍵。高級用戶可以通過取消勾選View下的Toolbar 菜單項來釋放屏幕空間。
The Navigation Bar
導(dǎo)航欄
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.
<翻譯>翻譯> 導(dǎo)航欄是以水平箭頭的鏈狀結(jié)構(gòu)方式來顯示從項目根目錄(左邊)依次到編輯器(右邊)中選中的選項卡。導(dǎo)航欄可以用來導(dǎo)航你項目中的資源文件而不必通過Project或者Commander工具窗口。
The Status Bar
狀態(tài)欄
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),狀態(tài)欄顯示一些相關(guān)的和上下文敏感反饋信息,比如正在運行中的進(jìn)程或者你項目中Git版本庫狀態(tài)的信息?,F(xiàn)在我們將詳細(xì)的討論一下狀態(tài)欄。
Figure 2-5. Status bar
圖2-5 狀態(tài)欄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.
<翻譯>翻譯> 在最左邊角落里的是邊欄切換按鈕。單擊此按鈕就可以隱藏或者顯示邊欄。另外,當(dāng)你的鼠標(biāo)懸停在上面的時候,會出現(xiàn)一個上下文菜單以便你激活任意一個工具窗口。
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.
<翻譯>翻譯> 消息區(qū)域用來提供反饋信息,同時顯示運行過程中的所有信息。當(dāng)你在UI上比如菜單項或者工具欄的按鈕上滾動鼠標(biāo)的時候,這塊區(qū)域會顯示相應(yīng)的提示。點擊這塊區(qū)域打開事件日志。
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.
<翻譯>翻譯> 編輯器光標(biāo)位置區(qū)域以行:列的形式顯示編輯器中光標(biāo)的位置。點擊這塊區(qū)域激活對話框來直接導(dǎo)航到你代碼中特定的行。
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.
<翻譯>翻譯> 行間隔符區(qū)域用來顯示你文本文件中的回車鍵格式。在Windows下默認(rèn)使用CRLF代表回車換行。如果是Unix和Mac機(jī)器的話則使用LF,Git中一樣。如果你在Windows電腦上開發(fā)的話,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.
<翻譯>翻譯> 文本格式區(qū)域用來源文件中的文本編碼格式。默認(rèn)是UTF-8,是ASCII的超集,它包含了大部分的西文字母和標(biāo)準(zhǔn)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.
<翻譯>翻譯> 文件訪問指示器區(qū)域能夠在讀/寫和只讀之間切換。解鎖圖標(biāo)意味著在當(dāng)前編輯器中具有讀/寫功能。鎖定圖標(biāo)意味著編輯器中的文件是只讀的。你可以通過點擊指示圖標(biāo)來切換設(shè)置。
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.
<翻譯>翻譯> 點擊高亮等級按鈕,激活一個帶滑塊按鈕的對話框,在這你可以設(shè)置代碼中高亮顯示的等級。
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.
<翻譯>翻譯> 默認(rèn)設(shè)置是Inspections,對應(yīng)一個皺著眉頭的審查經(jīng)理圖標(biāo)。此設(shè)置為了幫助你,審查經(jīng)理將對你代碼中的語法錯誤和被稱作警告的可能出現(xiàn)的問題進(jìn)行嚴(yán)格的檢查。你可以在標(biāo)記欄上看到審查經(jīng)理生成的黃色警告標(biāo)識。
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.
<翻譯>翻譯> 滑動塊的下一個設(shè)置是Syntax,對應(yīng)一個側(cè)著臉的審查經(jīng)理的圖標(biāo)。這個設(shè)置下,審查經(jīng)理是睜一只眼閉一只眼的。Syntax檢查不如Inspections嚴(yán)格,但是仍然會高亮顯示阻止代碼編譯的語法問題。
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,對應(yīng)一個微笑著的審查經(jīng)理圖標(biāo)。這個圖標(biāo)讓我想到審查經(jīng)理很開心并且不關(guān)心你的代碼。這種模式下,即使是最嚴(yán)重的語法錯誤也會被忽略,盡管當(dāng)你試圖構(gòu)建的時候編譯器仍然會阻止。我建議將高亮等級設(shè)置為Inspections并且學(xué)會欣賞審查經(jīng)理的“嚴(yán)厲的愛”。
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.
<翻譯>翻譯> 本節(jié)介紹在Android Studio中的各種常用操作。如果你使用過像微軟的Word類似的文本編輯器的話 ,那么你可能對這節(jié)比較熟悉。
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.
<翻譯>翻譯> 正如你想的那樣,任何一個好的文本編輯器,在源文件中雙擊任何單詞就能夠選中它。此外,單擊并拖動光標(biāo)選擇單詞或者字母中的文本元素。將光標(biāo)放在源文件中,并按下Shift+下箭頭或者Shift+上箭頭來選擇從光標(biāo)處開始的行文本。在行文本的任何地方三擊來選擇整行。按下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.
<翻譯>翻譯> 如果你將光標(biāo)放置在任何單詞上,并按下Ctrl+W|Alt+上箭頭將會選中整個單詞。如果你繼續(xù)按下Ctrl+W|Alt+上箭頭,將會無止境的選擇相鄰的文本。如果此時你按下Ctrl+Shift+W|Alt+下箭頭,文本選擇將會收縮。這種增加/減少選擇的功能在Android Studio中被稱為結(jié)構(gòu)化選擇。
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.
<翻譯>翻譯> 撤銷和重做命令對于回滾有限數(shù)量的編輯操作是非常有用的。特殊的UI事件比如按下Enter或者重新定位鼠標(biāo)用來限改變。撤銷和重做的快捷鍵分別是Ctrl+Z|Cmd+Z和Ctrl+Shift+Z|Cmd+Shift+Z。工具欄左邊的紫色左右箭頭也分別代表撤銷和重做。Android Studio會記住你上次保存至今的所有步驟或者達(dá)到上限300步。撤銷和重做只能在一段時間內(nèi)應(yīng)用到一個文件,因此,最有效方式來回滾更改是利用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來激活這個命令。結(jié)果對話框允許你選擇任何最近的文件,并將之作為編輯器中的一個選項卡來打開。默認(rèn)只能記住之前的50個文件。你可以通過File ?Settings ?Limits ?Editor ?Recent Files Limit來修改這些限制。
Traversing Recent Navigation Operations
遍歷最近導(dǎo)航操作
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也能夠記住你最近的導(dǎo)航操作。導(dǎo)航操作包括指針移動、標(biāo)簽變更和文件激活。按下Ctrl+Alt+左箭頭|Cmd+Alt+左箭頭或者Ctrl+Alt+右箭頭|Cmd+Alt+右箭頭來遍歷你的導(dǎo)航操作歷史。注意,導(dǎo)航操作不同于編輯操作;如果你想遍歷你的編輯操作,你應(yīng)該使用撤銷和重做。
Cutting, Copying, and Pasting
剪貼,復(fù)制,粘貼
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.
<翻譯>翻譯> 如果你使用過任何的文本編輯器或者文檔處理器,你應(yīng)該熟悉剪切、復(fù)制和粘貼。表2-2列出了這些基本命令以及一些擴(kuò)展的剪貼板命令。
Table 2-2. Cut, Copy, and Paste 表2-2 剪貼,復(fù)制,粘貼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.
<翻譯>翻譯> 除了協(xié)同剪貼板提供的一些簡單的剪切、復(fù)制和粘貼操作,Android Studio擴(kuò)展了剪貼板的功能,能夠記住之前五步剪貼和粘貼操作。在Android Studio運行的時候,當(dāng)你從Android Studio剪切或者復(fù)制文本(或者任何其他的應(yīng)用程序)的時候,Android Studio將會將這些文本放到一個堆棧中。按下Ctrl+Shift+V|Cmd+Shift+V就能夠看到擴(kuò)展的剪貼板堆棧。結(jié)果對話框允許你選擇粘貼其中一項。如圖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.
<翻譯>翻譯> 你也可以通過導(dǎo)航欄上的File ?Setting ?Limits ?Editor ?Maximum Number of Contents來更改擴(kuò)展剪貼板上內(nèi)容的數(shù)量。你也可以通過在選中的文本上右擊,然后選擇Compare with Clipboard項來將當(dāng)前選中的文本和剪貼板中最近的元素來進(jìn)行比較。
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.
<翻譯>翻譯> 復(fù)制路徑命令Ctrl+Shift+C|Cmd+Shift+C用來復(fù)制任何文件的物理路徑或者Project或Commander工具窗口中文件夾的路徑或者編輯器中選項卡的路徑。在終端會話中復(fù)制路徑操作是非常有用的。
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能夠復(fù)制一個邏輯引用到方法、變量或類中。當(dāng)你將這個引用復(fù)制到另一個源文件的時候,Android Studio會自動的包含所需的所有修飾符和imports。你也可以在Commander和Project工具窗口中的包、目錄和文件中使用剪切、復(fù)制和粘貼來代替鼠標(biāo)直接拖拽來整理你項目中的資源文件。
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.
<翻譯>翻譯> 大多數(shù)的上下文菜單都可以在IDE中通過右擊來激活(mac下按住ctrl單擊)。在前面的章節(jié),你已經(jīng)學(xué)習(xí)了編輯器選項卡的上下文菜單。Android Studio中的大多數(shù)的窗格,圖標(biāo)和邊欄,如果你右擊(mac下按住ctrl單擊)將會出現(xiàn)一個上下文菜單。Android Studio的最大優(yōu)勢是執(zhí)行一個操作僅僅一種方式。這種冗余意味著你可以根據(jù)你自己的喜好習(xí)慣來免費擴(kuò)展你的技能和習(xí)慣。我發(fā)現(xiàn)一些頻繁的操作使用快捷鍵,而對一些低頻率的操作使用菜單和上下文菜單進(jìn)行操作是非常有效的方法來使用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,然后使用鼠標(biāo)選擇Setting然后按下Enter。在設(shè)置窗口,選擇Editor ? Appearance。(譯者注:Android Studio是Editor》General》Appearance)。接著你就能看到Show Line Numbers的復(fù)選框了。
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中的所有技術(shù)規(guī)范文檔資源。這是Android Studio最全面的文檔。同時,Help?Default Keymap Reference菜單項也是一個非常有用的參考。你可以考慮將PDF文檔打印出來,然后在你學(xué)習(xí)使用Android Studio的時候放在手邊時時參考。
Navigating with the Keyboard
導(dǎo)航快捷鍵
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.
<翻譯>翻譯> 快捷鍵是最強(qiáng)大的方式來瀏覽Android Studio。在主菜單欄選擇Navigate菜單瀏覽其內(nèi)容。本節(jié)將討論一些重要的菜單項(如表2-3)及其對應(yīng)的導(dǎo)航菜單快捷鍵。隨后的章節(jié)討論其他的菜單項。
Table 2-3. Keyboard Navigation表2-3 導(dǎo)航快捷鍵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最好的特性之一是導(dǎo)航是雙邊的。你已經(jīng)見識過如何在各種工具窗口中作為編輯器的選項卡來打開/激活文件。現(xiàn)在你將要學(xué)習(xí)如何從編輯器中導(dǎo)航到 各種工具窗口。
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。激活選擇上下文菜單,其中包含幾個菜單項,包括項目視圖,收藏和文件結(jié)構(gòu)等。點擊項目視圖操作。項目工具窗口將會激活,文件對應(yīng)的編輯器中活動的選項卡將會高亮顯示,并且該文件的父目錄將會自動打開。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.
<翻譯>翻譯> 類操作允許你導(dǎo)航到一個特定的java類。值得重點關(guān)注的是這個操作只能查找Java資源文件或是Java資源文件的內(nèi)部類。按下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.
<翻譯>翻譯> 文件操作允許你導(dǎo)航到項目中的任何文件。如果你想查找你項目中的xml文件,那么你將用這個操作。按下Ctrl+Shift+N|Cmd+Shift+O然后輸入act。我們故意使用了相同的檢索項來說明Navigate》File中的更廣泛的范圍??梢宰⒁獾剿阉鹘Y(jié)果中包含Java資源文件MainActivity.java和一些其他的文件,如activity_main.xml。用 鼠標(biāo)來選擇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來執(zhí)行行操作,會激活一個對話窗口,然后以Line:Column形式導(dǎo)航到你的資源文件。如果你在Go to Line對話框中輸入一個簡單的整數(shù)然后點擊OK,Android Studio將會跳轉(zhuǎn)到指定的行而不管列。
Related File
相關(guān)文件
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+上箭頭來打開相關(guān)文件夾操作是Android Studio中一個非常有用的命令行。Android項目通常有一個非常多的相關(guān)文件。例如,一個簡單的Android Activity通常有至少一個相對應(yīng)的xml布局文件來呈現(xiàn)Activity的布局,和一個相對應(yīng)的XML菜單文件來呈現(xiàn)Activity的菜單。如果你使用Fragments,這只會增加復(fù)雜性。你已經(jīng)知道通過Favorites如何組織相關(guān)文件。Navigate ?Related File中,你可以看到Android Studio中展示給你的相關(guān)文件。打開MainActivity.java選項卡,按下Ctrl+Alt+Home|Alt+Cmd+上箭頭。你應(yīng)該能夠看到activity_main.xml列出來了。使用鼠標(biāo)來選擇然后按下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激活最后編輯位置,這個位置允許你導(dǎo)航到最后的編輯位置。如果你繼續(xù)激活這個命令,你的鼠標(biāo)將會移動到之前編輯的文件/位置,等等。
Type Hierarchy
類型層級結(jié)構(gòu)
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使用的是面向?qū)ο蟮木幊陶Z言Java。面向?qū)ο笳Z言的最大特點之一是繼承,它促進(jìn)了代碼的重用和多態(tài)性。在編輯器中打開MainActivity.java文件,按下Ctrl+H來切換打開層次結(jié)構(gòu)窗口。在打開的窗口你將看到一系列的層疊對象,所有的對象都可以追溯到祖先的在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.
<翻譯>翻譯> 聲明操作允許你跳轉(zhuǎn)到原始的定義方法、變量和資源。激活這個操作的另一種方式是按住Ctrl|Cmd然后在文件中的方法,變量或資源文件上滾動鼠標(biāo)。如果這個元素上有下劃線,你可以通過單擊導(dǎo)航到它的聲明上。在MainActivity.java中,點擊方法setContentView中的任何地方然后按下Ctrl+B|Cmd+B。你將會立即跳轉(zhuǎn)到該方法的聲明,它位于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有一套強(qiáng)大的工具來幫助你做到這一點。本節(jié)討論一些最重要的工具。如表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,你將注意到文件中會立即以黃色高亮顯示。你也可以在標(biāo)記欄上看到一些綠色的標(biāo)記用來標(biāo)記查找到的文本的位置。將鼠標(biāo)移到查找欄的雙右箭頭上將會顯示高級搜索選項。
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.
<翻譯>翻譯> 查找路徑操作比之前講的查找操作查找的范圍更廣泛的范圍。你也可以使用正則表達(dá)式和界定文件覆蓋的結(jié)果。按下Ctrl+Shift+F|Cmd+Shift+F并且在搜索框中輸入hello。默認(rèn)情況下,搜索范圍被設(shè)置為整個項目,但是你可以限制搜索范圍到一個特殊的目錄或是module。同意整個項目的默認(rèn)設(shè)置并且點擊查找按鈕。結(jié)果將會顯示在查找工具窗口。點擊查找窗口中的一個條目將會立即將關(guān)閉的文件打開為編輯器中一個新的選項卡并且跳轉(zhuǎn)到搜索的地方上。
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打開替換操作,這個操作用于替換當(dāng)前文件中的文本,并且替換功能是查找的超集。替換文本最安全的方法就是使用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會比在路徑中替換更好,因此,當(dāng)你引入錯誤的時候你將及其謹(jǐn)慎的使用這個命令。
Summary
總結(jié)
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.
<翻譯>翻譯> 在本章中,我們討論了編輯器和編輯器周圍的工具窗口。我們討論了如何使用工具按鈕和將他們重定位。我們也討論了包括主菜單欄,工具欄,狀態(tài)欄,邊欄和標(biāo)記欄的用于導(dǎo)航的工具窗口和主要的UI元素。我們也討論了如何去搜索和通過使用菜單欄和快捷鍵來導(dǎo)航,以及使用查找和替換。最后,我們討論了Android Studio中怎樣使用幫助系統(tǒng)。更重要的是,我們已經(jīng)在Android Studio中建立了一個UI元素,這些我們將在后續(xù)章節(jié)中引用。
The Main Menu Bar主菜單欄The main menu bar is the uppermost bar in Android Studio, and you can execute virtually any action by navigat...