Android Studio code formatting issue

喜欢而已 提交于 2019-12-13 01:47:37

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/31369057/android-studio-code-formatting-issue

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