How to align/format code in Android Studio?

2019-03-22 17:24发布

问题:

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 user to select code that needs to be aligned and use 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.

Thanks in advance! :)

回答1:

Indent code in Android Studio:


Windows

Ctrl+Alt+L


Mac:

Option + Command + L



回答2:

In Eclipse which comes with Android Studio Bundle the short cut key is as follow to initiate Auto Formatter

ctrl+alt+L

If you want to change this Combination goto Preferences > Keymap and set to custom



回答3:

The alignment for variable declarations works for only those that are outside of functions. The Alignment feature in the Preference -> Code Style -> Wrapping and Braces -> Fields Groups -> "Align in Columns" does not work for those inside the brackets/braces.

Here is a Issue Ticket on 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.

Hope this helps



回答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