How do I align/format code in Android Studio?

十年热恋 提交于 2019-12-04 15:19:02

问题


Is there a way/shortcut/built-in feature that can align code for operands, like '=' signs?

For example, there is a XAlign for Xcode (https://github.com/qfish/XAlign), allowing the user to select code that needs to be aligned and use a shortcut to align it automatically.

There is a Fields Group feature in Android Studio which can "Align in columns", but it does not work for code that is already written.


回答1:


Indent code in Android Studio:


Windows

Ctrl + Alt + L


Mac:

Option + Command + L




回答2:


In Eclipse which comes with Android Studio Bundle, the shortcut key is as follows to initiate Auto Formatter:

Ctrl + alt + L

If you want to change this combination, go to menu PreferencesKeymap and set to custom.




回答3:


The alignment for variable declarations works for only those that are outside of functions. The alignment feature in menu PreferenceCode StyleWrapping and BracesFields Groups"Align in Columns" does not work for those inside the brackets/braces.

Here is an issue ticket on the JetBrains website: https://youtrack.jetbrains.com/issue/IDEA-56947#

This feature is available, but not in the stable release yet. It is available from build: 140.1761

At the time of writing this answer my current build is "135.1653844, built on December 18, 2014" on Stable Release.




回答4:


For Windows and Linux in Android Studio:

Ctrl + Alt + L

For Mac:

Option + Command + L




回答5:


Windows:

Ctrl+Alt+L

Linux:

Ctrl+Shift+Alt+L

macOS:

⌥ option+⌘ command+L




回答6:


Ubuntu Linux: Ctrl-alt-L (suggested by other responses) is captured by the window manager and locks the screen. So you need to change it: file->settings->keymap->main menu->code->Reformat Code. I use a double tab since I don't want the tab to shift a selection right.

This formats the code as you've specified in your preferences. To handle your specific request (aligning equals statements) go to: (file->settings->editor->code style->(choose language)->Wrapping and braces->Group declarations->align variables in columns

These styles are language specific so this particular option might not exist in the language you chose, or may be named differently.




回答7:


Windows:

CTRL-ALT-L

Linux:

CTRL-SHIFT-ALT-L

macOS:

OPTION-COMMAND-L


来源:https://stackoverflow.com/questions/28223188/how-do-i-align-format-code-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!