简体   繁体   中英

Android Studio code formatting issue

I am using Android Studio and having an issue when formatting the code. When there is a statement that breaks into multiple lines and I need to format that to a single line statement, I can't do it using (Ctrl+Alt+L). The code didn't format to a single line using that shortcut.

I tried changing the value of Right margin (Settings -> Editor -> Code Style) to a higher number, but that also didn't work.

How to format the code to a single line?

Thanks.

First go to Preferences > Editor > Code Style and set Right margin (columns) to 140 (or higher)

Now go to Preferences > Editor > Code Style > Java > Wrapping and Braces > Keep when reformatting and uncheck the checkbox Line Breaks , so the formatter will remove unnecessary line breaks and make the statement fit as much as possible in one single line

Also recommended, in Preferences > Editor > Code Style > Java > Wrapping and Braces make sure to check the checkbox Ensure right margin is not exceeded so that the formatter will force a line break when the statement exceeds the maximum right margin.

Is the code wrapping into the next line? If so, you can disable soft wrapping ins View -> Active Editor -> Use Soft Wraps . The code is still considered on the same line with or without soft wraps, the editor just displays it differently.

If not, try editing the reformatting settings. Under Preferences, go to Editor -> Code Style and select the language. There is a tab Wrapping and Braces that let you customize the rules Android Studio will use when formatting the code.

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