在 Eclipse 中格式化 XML 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4527474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format XML code in Eclipse
提问by sandalone
When I write .xml files in Eclipse, I often have to manually indent code. For example
当我在 Eclipse 中编写 .xml 文件时,我经常需要手动缩进代码。例如
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
After formatting it looks like this.
格式化后是这个样子。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
How to automate this? I can't find formatting settings in Eclipse for XML.
如何自动化?我在 Eclipse 中找不到 XML 的格式设置。
回答by Denis Palnitsky
Ctrl+Shift+F
Ctrl+ Shift+F
Will do it for you, and you can configure it in
会为你做,你可以在
Window -> Preferences -> XML -> Xml Files -> Editor
回答by Paul
As stated above, Ctrl+Shift+Fwill auto-format. However, by default Eclipse will split attributes in a naive way. If you're like me and prefer to have all the attributes on their own lines, go to Window→Preferences→XML→XML Files→Editor and check Split multiple attributes each on a new line.
如上所述,说明Ctrl+ Shift+F会自动格式。但是,默认情况下 Eclipse 会以一种幼稚的方式拆分属性。如果您像我一样喜欢将所有属性都放在自己的行上,请转到 Window→Preferences→XML→XML Files→Editor 并选中Split multiple attributes each on a new line。
回答by madoke
CMD + SHIFT + F for Macintosh users
Macintosh 用户的 CMD + SHIFT + F
回答by Robert Baldock
You need to open the file using the XML Editor, swap to Source view and then do the Ctrl+ Shift+ Ftrick.
您需要使用 XML 编辑器打开文件,切换到源视图,然后执行Ctrl+ Shift+F技巧。
回答by sudhir
open windows->preferences->xml files->editor enter some high number for line width, say 999 and then format the xml file with ctrl+shift+F.
打开 windows->preferences->xml files->editor 为line width输入一些大数字,比如 999,然后用 ctrl+shift+F 格式化 xml 文件。
You ll see the lines are aligned properly.
您会看到线条已正确对齐。
回答by Kalyan
You can set the default XML editor to “Java Editor”:
您可以将默认 XML 编辑器设置为“Java 编辑器”:
- Right-click on the XML
- Open With
- Other...
- Java Editor
- 右键单击 XML
- 打开用
- 其他...
- Java 编辑器
This applies all java formatting and controls over XML file.
这适用于 XML 文件的所有 Java 格式和控制。

