简体   繁体   English

如何将标签转换为空格并在android studio中保存时自动格式化

[英]How to convert tabs to spaces and auto formatting on save in android studio

I would like know the steps for converting tabs to 4 spaces and auto formatting the java code on Save which is similar to eclipse. 我想知道将标签转换为4个空格并在Save上自动格式化java代码的步骤,类似于eclipse。

In Eclipse I used to do the settings 在Eclipse中我曾经做过设置

For Converting tabs to Spaces I used to do the below setting 对于将标签转换为空格我曾经做过以下设置

Open references-> Java->Code style -> Formatter->Create a new profile-> Change the setting from tabs only to Spaces only 打开引用 - > Java->代码样式 - > Formatter->创建新的配置文件 - >仅将选项卡的选项更改为仅限空格

To format the entire Java code of that particular file on save I used to do the below settings in Eclipse 要在保存时格式化该特定文件的整个Java代码,我以前在Eclipse中执行以下设置

Open preferences-> Java -> Editor -> Save Actions->Check the appropriate options in the dialog 打开首选项 - > Java - >编辑器 - >保存操作 - >选中对话框中的相应选项

But I don't aware to do the similar settings in Android Studio, can anyone help me to setup this. 但是我不知道在Android Studio中进行类似的设置,任何人都可以帮我设置它。 Thanks in advance. 提前致谢。

For Auto Formatting your Java/xml files in Android Studio use following commands: 对于在Android Studio中自动格式化Java / xml文件,请使用以下命令:

CTRL + ALT + L CTRL + ALT + L.

If you are using ubuntu , using this command will Log you out, use : 如果您使用的是ubuntu,使用此命令将退出,请使用:

Shift + CTRL + ALT + L Shift + CTRL + ALT + L.

A popUp will appear : select whole file and Rearrange code and You are done. 将出现一个popUp:选择整个文件并重新排列代码,您就完成了。 :) :)

Update : 更新:

As you are looking for a single point to go with your work as eclipse did, there is no such built in functionality in Android studio but it gives you the ability to do it through Macro , Follow these steps: 正如您正在寻找与eclipse一样的单点工作, Android studio没有这样的内置功能,但它使您能够通过Macro执行此操作,请按照下列步骤操作:

  1. In studio open any of your source code file. 在工作室中打开任何源代码文件。
  2. Press Ctrl + Alt + O This is used to find/optimize unused import. Ctrl + Alt + O这用于查找/优化未使用的导入。 If a dialog open select 'Dont show it again' and hit Run. 如果打开一个对话框,请选择“不再显示”并点击“运行”。
  3. Go to Edit > Macros > Start Macro Recording. 转到编辑>宏>开始宏录制。
  4. Press Ctrl + Alt + O . Ctrl + Alt + O
  5. Press Ctrl + Alt + L . Ctrl + Alt + L This formats your code. 这会格式化您的代码。 ( Shift+ctrl+Alt+L for ubuntu). (对于ubuntu, Shift+ctrl+Alt+L )。
  6. Press Ctrl + S ,This saves your file;) Ctrl + S ,这将保存您的文件;)
  7. Go to Edit > Macros > Stop Macro Recording. 转到编辑>宏>停止宏录制。
  8. Save this created macro with name for instance:“Auto Formatting". 使用名称保存此创建的宏,例如:“自动格式化”。

Boom, You have now successfully created your Macro, Now just add/assign a shortcut to it: Boom,您现在已经成功创建了宏,现在只需添加/指定它的快捷方式:

  1. Open Preferences. 打开首选项。
  2. Search in the left bar for Keymap. 在左侧栏中搜索Keymap。
  3. In the right hand pane, click in the search bar and type the name of your saved macro("Auto Formatting".) 在右侧窗格中,单击搜索栏并键入已保存宏的名称(“自动格式化”。)
  4. Double click on your item. 双击您的项目。 There might be two, it doesn't matter which one you click on. 可能有两个,你点击哪一个并不重要。
  5. Click Add Keyboard Shortcut. 单击“添加键盘快捷键”。
  6. Set your keyboard shortcut to Ctrl + S . 将键盘快捷键设置为Ctrl + S
  7. Finally,Confirm overriding Ctrl + S to be your new macro. 最后,确认覆盖Ctrl + S作为新宏。

That's it! 而已! your done man. 你做完的男人。 Cheers :) 干杯:)

Convert tab into spaces and vice versa 将制表符转换为空格,反之亦然

Type in spac in the search panel of the settings view. 在设置视图的搜索面板中键入spac

Here you will find a shortcut to Editor -> Code Style -> Java -> Tabs and Indents -> Use tab character 在这里,您可以找到Editor -> Code Style -> Java -> Tabs and Indents -> Use tab character的快捷方式

AutoIndent 自动缩进

Simply CTRL + ALT + L 只需CTRL + ALT + L

In the former replay just replace 在前重播只是替换

  1. Open Preferences with "Open the keymap settings, click File > Settings > Keymap (on Mac, File > Properties > Keymap)." 打开首选项 “打开键盘映射设置,单击文件>设置>键盘映射(在Mac上,文件>属性>键盘映射)。”

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

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