当前位置:首页 > 开发教程 > 手机开发 >

Android 提示于技巧Android Studio Tips and Tricks

时间:2016-05-09 14:27 来源:互联网 作者:源码搜藏 收藏

Android Studio Tips and Tricks Android 提示于技巧 If youre unfamiliar with using Android Studio and the IntelliJ IDEA interface, this page provides some tips to help you get started with some of the most common tasks and productivity enhan

Android Studio Tips and Tricks

Android 提示于技巧

If you're unfamiliar with using Android Studio and the IntelliJ IDEA interface, this page provides some tips to help you get started with some of the most common tasks and productivity enhancements.

如果你对Android Studio 和Intellij IDEA 的界面很陌生,刚好这篇文章提供一些技巧教你一些最常见的技巧来使你提高生产率。

Productivity Shortcuts

高效率快捷键

Android Studio includes a number of features to help you be more productive in your coding. This section notes a few of the key features to help you work quickly and efficiently.

Android Studio 包括了许多功能,来帮助你的编程效率更高。

Smart Rendering

智能渲染

With smart rendering, Android Studio displays links for quick fixes to rendering errors. For example, if you add a button to the layout without specifying the width and height attributes, Android Studio displays the rendering message Automatically add all missing attributes. Clicking the message adds the missing attributes to the layout.

随着智能渲染,Android Studio 中显示快速修复渲染的错误的链接。

Bitmap rendering in the debugger

在调试中渲染Bitmap

While debugging, you can now right-click on bitmap variables in your app and invoke View Bitmap. This fetches the associated data from the debugged process and renders the bitmap in the debugger.

当你在调试中,你可以右键点击Bitmap 变量在你的App 来调用Bitmap 视图。

这将提取从调试的过程相关数据,并显示调试器中的Bitmap 中。

Android 提示于技巧Android Studio Tips and Tricks

Figure 1. Bitmap Rendering

Creating new files

创建新文件

You can quickly add new code and resource files by clicking the appropriate directory in the Project pane and pressingALT + INSERT on Windows and Linux or COMMAND + N on Mac. Based on the type of directory selected, Android Studio offers to create the appropriate file type.

你可以通过点击Project 面板中相应的文件夹中,通过输入“Alt + Insert”(Windows/Linux) ,或者Command + N(Mac),来快速添加新的代码和资源文件。根据所选中的文件夹类型,Android Studio 会提供创建适当的文件类型。

For example, if you select a layout directory, press ALT + INSERT on Windows, and select Layout resource file, a dialog opens so you can name the file (you can exclude the .xml suffix) and choose a root view element. The editor then switches to the layout design editor so you can begin designing your layout.

例如,如果你选中一个布局文件夹,键入“Alt + Insert”(在Windows),和选中布局资源文件,打开一个对话框,你可以输入一个文件的名字(可以省去.xml 后缀),然后选择根节点,编辑后会切换到设计编辑器中,你可以开始设计布局。

Output window message filtering

输出框信息过滤

When checking build results, you can filter messages by message type to quickly locate messages of interest.

当你检查build 结果,你可以按照消息类型过滤消息快速找到感兴趣的消息。

Figure 2. Filter Build Messages

Hierarchical parent setting

父层次设置

The activity parent can now be set in the Activity Wizard when creating a new activity. Setting a hierarchal parent sets theUp button to automatically appear in the app's Action bar when viewing a child activity, so the Up button no longer needs to be manually specified in the menu.xml file.

Activity 上一个Activity 可以通过在一个Activity 向导中创建一个新的Activity 中设置。

设置父层次通过返回按钮自动引入在你的应用中的ActionBar 中当Activity 创建子View 时。???

因此返回按钮不需要在“menu.xml” 中手动指定了。

Creating layouts

创建布局

Android Studio offers an advanced layout editor that allows you to drag-and-drop widgets into your layout and preview your layout while editing the XML.

Android Studio 提供了一个高级的布局编辑器 允许你拖拽控件到你的布局中。在预览布局中编辑XML 。

While editing in the Text view, you can preview the layout on devices by opening the Preview pane available on the right side of the window. Within the Preview pane, you can modify the preview by changing various options at the top of the pane, including the preview device, layout theme, platform version and more. To preview the layout on multiple devices simultaneously, select Preview All Screen Sizes from the device drop-down.

当在编辑Text 视图中,你可以打开预览面板在窗口的右侧预览设置上的布局。你可以通过修改面板顶部改变各种选项来修改预览,包括预览设备,主题,平台版本等等,可以同时预览多个设备上布局,可以从“设备”选项中下拉"预览所有屏幕的尺寸" 的选项。

Android 提示于技巧Android Studio Tips and Tricks

Figure 3. Preview All Screens

You can switch to the graphical editor by clicking Design at the bottom of the window. While editing in the Design view, you can show and hide the widgets available to drag-and-drop by clicking Palette on the left side of the window. ClickingDesigner on the right side of the window reveals a panel with a layout hierarchy and a list of properties for each view in the layout.

你可以通过点击底部的Design 窗口来切换图形编辑器。当编辑是Desin 视图时,你可以通过点击左边的窗口的面板拖拽来显示和隐藏控件。点击Designer 右边窗口的显示面板,显示布局的层次和在布局文件中每个View 的属性列表。

Annotations

注解

Android Studio provides coding assistance for using annotations from the Support-Annotations library, part of the Support Repository. Adding a dependency for this library enables you to decorate your code with annotations to help catch bugs, such as null pointer exceptions and resource type conflicts. You can also create enumerated annotations to, for example, check that a passed parameter value matches a value from a defined set of constants. For more information, see Improving Code Inspection with Annotations.

Android Studio 提供了 Support-Annotations 库帮助你在代码中使用注解,它属于支持库的一部分,添加依赖这个库可以装饰你的代码,帮助你捕获Bug,比如空指针异常,资源类型冲突,你也可以创建枚举注解,例如,从一组定义的常量中检查传入的参数值,更多请看 改善与标注的代码检查。

Java class decompiling

Java 类反编译

Android Studio allows you to look at what’s inside Java libraries when you don’t have access to the source code.

当你没有访问到源代码时,Android Studio 允许你查看Java 库里有设了。

The decompiler is built into Android Studio for easy access. To use this feature, right-click a class, method, or field from a library for which you do not have source file access and select decompile.

内置反编译让Android Studio 中很方便,右击一个类,方法或者字段,当你没有源文件并且选择反编译。

The decompiled source code appears.

反编译的代码会出现。

To adjust the Java decompiler settings, select File > Settings > Other Settings > Java Decompiler.

要调整反编译的设置,选择 File > Settings > Other Settings > Java Decompiler

Debugging and performance enhancements

调试和性能增强

Android Studio offers debugging and performance enhancements such as:

  • Custom keymaps. To modify the current keymap, choose File > Settings > Keymap.

  • Support for high density (Retina) displays on Windows and Linux.

  • Scratch files for quick prototyping without creating any project files.

Choose Tools > New Scratch File to open a scratch file to quickly build and run code prototypes. Together with Android Studio coding assistance, scratch files allow you to quickly run and debug code updates with the support of all file operations. By embedding code created with scripting languages, you can run your code from within the scratch file.

Android Studio 提供了调试和性能增强,比如:

  • 自定义快捷键,更改当前的快捷键,选择 File > Settings > Keymap

  • 支持高分辨率(视网膜)显示在Widnows 和Linux。

  • Scratch 文件快速原型文件,不会创造任何项目文件。

选择Tools > New Scratch File 打开一个Scratch 文件快速构建和运行代码原型。再加上Android Studio 的代码辅助,Scratch 文件允许你快速运行和支持所有文件类型的调试代码更新。通过嵌入脚本语言创建的代码,你可以从临时文件中运行代码。

Live templates

动态模板

Live templates allow you to enter code snippets for fast insertion and completion of small chunks of code. To insert a live template, type the template abbreviations and press the Tab key. Android Studio inserts the code snippet associated with the template into your code.

动态模板允许你输入代码片段快速插入和完成小块的代码。插入动态模板输入模板的缩写点击Tab 键,Android Studio 中插入代码片段到相关联的代码中。

For example, entering the newInstance abbreviation followed by the Tab key inserts the code for a new fragment instance with argument placeholders.

例如,输入 newInstance 缩写,按Tab 键将插入一个新初始化包括参数占位符的Fragment 。

public static $fragment$ newInstance($args$) {
    $nullChecks$
    Bundle args = new Bundle();
    $addArgs$
    $fragment$ fragment = new $fragment$();
    fragment.setArguments(args);
    return fragment;
}

Similarly, the fbc abbreviation inserts a findViewById call along with cast and resource id syntax.

相似的,输入 fbc 将插入 findViewById 将调用将资源Id 要转换的类型。

() findViewById(R.id.);

Use the File > Settings > Editor > Live Templates menu option to display the full list of supported live templates and customize the inserted code.

用 File > Settings > Editor > Live Templates 菜单选项来显示,支持实时模板和自定义插入代码。

Working with IntelliJ-based Coding Practices

基于的IntelliJ编码实践工作

This section list just a few of the code editing practices you should consider using when creating Android Studio apps.

For complete user documentation for the IntelliJ IDEA interface (upon which Android Studio is based), refer to the IntelliJ IDEA documentation.

本节仅仅是一小部分代码编辑实践基于的IntelliJ编码实践工作。当你在使用Android Studio 时你应该考虑使用它。

想要了解IntelliJ IDEA的界面完整的用户文档(Android Studio 是基于它的)请参考 IntelliJ IDEA 文档。

Alt + Enter key binding

For quick fixes to coding errors, the IntelliJ powered IDE implements the Alt + Enter key binding to fix errors (missing imports, variable assignments, missing references, etc) when possible, and if not, suggest the most probable solution.

快速修复代码中的错误,强大的Intellij 实现了Alt + Enter 修复错误(遗漏的导入,变量赋值,丢失的应用等)在可能的情况下,如果不可以,选择最有可能解决问题的建议。

Ctrl + D key binding

The Ctrl + D key binding is great for quickly duplicating code lines or fragments. Simply select the desired line or fragment and enter this key binding.

Ctrl + D 是一个强大的快速赋值代码行和片段,只需要选择 所需的行或者片段,然后输入这个快捷键(Ctrl + D)。

Navigate menu

导航菜单

In case you're not familiar with an API class, file or symbol, the Navigate menu lets you jump directly to the class of a method or field name without having to search through individual classes.

如果你不熟悉一些API 的类,文件或者标识,导航菜单可以可以直接跳转类的方法,字段,而不需要你在手动去搜索。

Inspection scopes

范围检查

Scopes set the color of code segments for easy code identification and location. For example, you can set a scope to identify all code related to a specific action bar.

范围指定的更容易识别和定位代码段的颜色,例如,你可以设定一个范围的ID,以确定和特定ActionBar 的所有代码。???

Injecting languages

注入语言

With language injection, the Android Studio IDE allows you to work with islands of different languages embedded in the source code. This extends the syntax, error highlighting and coding assistance to the embedded language. This can be especially useful for checking regular expression values inline and validating XML.

使用嵌入语言,Android Studio 允许你在工作区中嵌入不同的语言,这个语法的延伸,错误高亮和嵌入语言的编码帮助。这对于内嵌检查正则表达式的值和验证XML 是非常有用的。

Code folding

代码折叠

This allows you to selectively hide and display sections of the code for readability. For example, resource expressions or code for a nested class can be folded or hidden in to one line to make the outer class structure easier to read. The inner class can be later expanded for updates.

这让你可以选择性地隐藏和显示部分可读代码。例如资源表达式或者内嵌代码可以被折叠或隐藏成一行,使得外部的结构更加容易阅读。

Image and color preview

图片和颜色预览

When referencing images and icons in your code, a preview of the image or icon appears (in actual size at different densities) in the code margin to help you verify the image or icon reference. Pressing F1 with the preview image or icon selected displays resource asset details, such as the dp settings.

当你在代码中引用图片或者图标,会在代码边上显示图片或者图标的预览(根据实际尺寸的不同密度),以帮助你验证图像或图标参考。按 F1 键在预览图片或者图标显示资源文件的详细信息。例如dp 的设置。

Quick F1 documentation

快速查找文档

You can now inspect theme attributes using View > Quick Documentation (F1), see the theme inheritance hierarchy, and resolve values for the various attributes.

If you invoke View > Quick Documentation (usually bound to F1) on the theme attribute android:textAppearanceLarge, you will see the theme inheritance hierarchy and resolved values for the various attributes that are pulled in.

你可以检查使用的主题属性使用 View > Quick Documentation (F1),查看主题的的继承层次,并解决各种属性值。

如果你调用 View > Quick Documentation (usually bound to F1)来查看android:textAppearanceLarge 的主题属性值。你将会看到主题的继承层次,显示出各种属性解析值。

Keyboard Commands

快捷键

The following tables list keyboard shortcuts for common operations.

Note: This section lists Android Studio keyboard shortcuts for the default keymap. To change the default keymap on Windows and Linux, go to File > Settings > Keymap. If you're using Mac OS X, update your keymap to use the Mac OS X 10.5+ version keymaps under Android Studio > Preferences > Keymap.

下表列出了常用的键盘快捷键。修改默认的快捷键可以在:File > Settings > Keymap(Windows/Linux)。如果你使用Mac OS X,更新你的款借鉴必须使用Mac OS X 10.5+ 的版本在: Android Studio > Preferences > Keymap

Table 1. Programming key commands

编程键盘命令

Action Android Studio Key Command
命令查找 CTRL + SHIFT + A
工程快速修复 ALT + ENTER
格式化代码 CTRL + ALT + L (Win)OPTION + CMD + L (Mac)
显示文档 CTRL + Q (Win)F1 (Mac)
显示选中方法的参数 CTRL + P
生成方法 ALT + Insert (Win)CMD + N (Mac)
跳转到源码 F4 (Win)CMD + down-arrow (Mac)
删除行 CTRL + Y (Win)CMD + Backspace (Mac)
通过符号名称搜索 CTRL + ALT + SHIFT + N (Win)OPTION + CMD + O (Mac)

Table 2. Project and editor key commands

项目和编辑快捷键

Action Android Studio Key Command
构建 CTRL + F9 (Win)CMD + F9 (Mac)
构建和运行 SHIFT + F10 (Win)CTRL + R (Mac)
切换项目可见 ALT + 1 (Win)CMD + 1 (Mac)
导航打开的标签页 ALT + left-arrow; ALT + right-arrow (Win)CTRL + left-arrow; CTRL + right-arrow (Mac)

For a complete keymap reference guide, see the IntelliJ IDEA documentation.

更多的快捷键完全参考指南,请看IntelliJ IDEA 文档。


手机开发阅读排行

最新文章