在Eclipse中格式化XML代码

当我在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" /> 

格式化后它看起来像这样。

 <?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" /> 

如何自动化呢? 我无法在Eclipse for XML中find格式设置。

Ctrl + Shift + F

将为你做,你可以configuration它

 Window -> Preferences -> XML -> Xml Files -> Editor 

如上所述, Ctrl + Shift + F将自动格式化。 但是,默认情况下,Eclipse将以一种天真的方式分割属性。 如果你像我一样喜欢自己的所有属性,请转到Window&rightarrow; Preferences&rightarrow; XML&rightarrow; XML Files&rightarrow; Editor并检查在一个新行上拆分多个属性

用于Macintosh用户的CMD + SHIFT + F

您需要使用XML编辑器打开文件,切换到源视图,然后执行Ctrl + Shift + F技巧。

打开windows-> preferences-> xml文件 – >编辑器为线宽input一些高数字,比如999,然后用ctrl + shift + F格式化xml文件。

你会看到线正确alignment。

如上所述,只有当我们在一个新行上检查拆分多个属性时,Ctrl + Shift + F才会在Eclipse或STS中自动格式化。 要检查这个窗口→首选项→XML→XML文件→编辑器。

Interesting Posts