简体   繁体   中英

How to format code in Android Studio (IntelliJ IDEA)

I want to format code in a freshly created Android application.

I messed up the code in this manner:

 private static final
    int AUTO_HIDE_DELAY_MILLIS
            = 3000;

I want to format it with Ctrl + Alt + L , but it is not repairing the code. I use Windows 8.1.

Just right-click on the project or directory, and select Reformat Code重新格式化代码

If you want to:

  1. Order/FormatCode: Ctrl + Alt + L
  2. Implement Methods: Ctrl + I
  3. Override Methods: Ctrl + O

There isn't any need for them to be capital letters. This is just for the example.

First you will need to configure how your code is formatted. This can be done by going to

[Main Menu] -> File -> Settings
then go to
(Project Settings) -> Code Style -> Java

In this area you can define all the formatting rules that you would like to apply to your code.

Now you should check (or change) the keyboard shortcut for reformatting code by going to

[Main Menu] -> File -> Settings
then go to
(IDE Settings) -> Keymap

Just type Reformat Code into the search box at the top right to find it.

Once this is done, highlight the code you want to reformat and use the keyboard shortcut to reformat your code based on the rules you defined.

In Android Studio (on the Mac), I select the code chunk, and then select the Code menu item and click Auto-Indent Lines .

See screenshot:

在此处输入图片说明

Shortcut key (on Mac) is Ctrl + Alt + I .

At least on my Android Studio version, "keep when formatting - line breaks" was enabled by default. So you need to go to

PreferencesEditorCode StyleJava and disable that checkbox (see the below screenshot).

Android Studio 偏好设置

Alt + Ctrl + Shift + L是在 Ubuntu (Android Studio) 中整理代码的快捷键。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM