简体   繁体   English

Android Studio代码格式问题

[英]Android Studio code formatting issue

I am using Android Studio and having an issue when formatting the code. 我正在使用Android Studio,并且在格式化代码时遇到问题。 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). 当有一条语句分成多行并且需要将其格式化为单行语句时,我无法使用(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) 首先转到Preferences > Editor > Code Style然后将“ Right margin (columns)140 (或更高)

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 现在转到Preferences > Editor > Code Style > Java > Wrapping and Braces > Keep when reformatting然后uncheckLine Breaks ”复选框,这样格式化程序将删除不必要的换行符并使该语句尽可能地适合一行

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. 同时建议,在Preferences > Editor > Code Style > Java > Wrapping and Braces确保check该复选框Ensure right margin is not exceeded这样当声明超过最大右边距格式化将强制断线。

Is the code wrapping into the next line? 代码是否包装在下一行中? If so, you can disable soft wrapping ins View -> Active Editor -> Use Soft Wraps . 如果是这样,您可以禁用软包装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. 在首选项下,转到Editor -> Code Style然后选择语言。 There is a tab Wrapping and Braces that let you customize the rules Android Studio will use when formatting the code. 有一个“ Wrapping and Braces标签,可让您自定义Android Studio在格式化代码时将使用的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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