Eclipse格式化设置。。。

来源:互联网 发布:et纸样软件下载 编辑:程序博客网 时间:2024/06/14 08:21

Eclipse 格式化代码块快捷键:Ctrl+Shift+F

但对于XML文件来说有时格式化后,一个控件的配置属性全都挤在一行,阅读和修改起来都很不方便。

而我们希望最佳的格式化效果如下,控件的每个属性配置占一行方便阅读修改,没有子元素的控件直接使用/> 关闭:

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"           
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

         <TextView android:id="@+id/TextView01"

         android:text="Some Content"

         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>

   为了达到上述的格式化效果,需要修改Eclipse的一些配置及进行一些额外的操作:

1)修改Eclipse的XML格式化配置
   这一步的配置是使格式化的效果为控件的每个属性配置占一行。进入Window/Preferences,展开到 XML/XML Files/Editor,
勾选 “Split multiple attributeseach on a new line” 即可,如下图所示:

Eclipse格式化设置。。。


 经此配置后,每次使用快捷键 Ctrl+Shift+F 键格式化后每个属性配置就会占一行。

2)压缩节点的声明方式
   这步的目的是将没有子节点的元素的声明方式进行压缩,如将“<TextView...></TextView>” 转化为“<TextView .../>”。
方法为在XML文件内空白地方点击鼠标右键,选择”Source/Cleanup Document...“,如下图所示:
Eclipse格式化设置。。。
之后将弹出如下界面:

Eclipse格式化设置。。。


 勾选“Compress empty element tags”,点击 OK按钮即可。