`

Auto Formatting Android XML Files With Eclipse

 
阅读更多

http://www.androidpolice.com/2009/11/04/auto-formatting-android-xml-files-with-eclipse/

http://envyandroid.com/archives/195/formatting-android-xml-eclipse

The Problem

However, one thing about Eclipse Android development has bothered me for a while - and that is XML formatting by the visual tools. See, if you open an XML file in Eclipse and use the Layout/Resources tab (which functions as either a visual UI builder or a visual frontend to resource management), Eclipse creates the XML representation of what you're building and dumps it into the file you're editing. The problem is, this XML is not formatted in any way - Eclipse just writes it in a single line, which looks absolutely horrible and is essentially unusable.

For example, I had this beautifully formatted XML file with a sample Android menu. After using the Layout tab to add another item, I ended up with an single ugly unindented line for it.

image

The Solution

Here's how to solve this in an almost fully automated fashion, using formatting rules that I think result in the most readable file. There is no need for external tools - Eclipse provides everything needed.

  • open up Window->Preferences->XML->XML Files->Editor 
    image
  • check the "Split multiple attributes each on a new line" and adjust other options according to your liking (such as Indent using spaces and Indentation size)
  • press OK to save the options
  • all you have to do now is press Ctrl-Shift-F or select Source->Format

Let's see what this does to the XML I showed above:

image

It's a beauty, isn't it? Eclipse's Auto Format is one of its best features (yes, you can apply it to your Android Java code too as well as pretty much any other document format).

Bonus - XML Cleanup

As an added bonus, Eclipse also offers a Cleanup XML option, which can do the following:

  • compress empty element tags
  • insert required attributes
  • insert missing tags
  • quote attribute values
  • format source (as above)
  • convert line delimiters to Windows, UNIX, or Mac

You can access the Cleanup option via Source->Cleanup

imageRunning Cleanup on my already pretty clean XML file turned the last <item> into this:

image

The first option compressed the <item></item> declaration to simply <item />. Neat, isn't it?

Final Words

+ Clean is good.

+ Uniform is good.

+ Use Ctrl-Shift-F and make it a habit.

+ Create a keyboard shortcut to the Cleanup function by going to Window->Preferences->General->Keys and use it instead of Ctrl-Shift-F (since it already formats as part of the Cleanup).

? I'd like to know if there is a way to apply auto formatting upon save, automatically. Do you?

分享到:
评论

相关推荐

    System.Net.Http.Formatting.xml

    System.Net.Http.Formatting.xml

    Android和XML代码格式化

    Eclipse 中: 配置方法: window-&gt;preferences-&gt;java-&gt;Code style-&gt;Formatter中导入android-formatting.xml ...http://www.androidpolice.com/2009/11/04/auto-formatting-android-xml-files-with-eclipse/

    Eclipse来开发Android源码

    - 可选步骤:导入`Android-formatting.xml`和`android.importorder`,分别用于定义代码风格和导入顺序。这些文件通常位于`development/ide/eclipse/`目录下。 - 安装AnyEdit插件(可选),这是一个增强Eclipse功能...

    在Eclipse里关联android源代码的三种方式

    然后,导入`Android-formatting.xml`和`android.importorder`文件,分别用于设置代码格式和导入顺序。最后,安装像AnyEdit这样的Eclipse插件,以增强代码编辑功能。 为了导入源码,确保你已经备份了源码,因为导入...

    怎样使用Eclipse来开发Android源码

    3. **导入编码规范文件**:将`android-formatting.xml`和`android.importorder`文件导入Eclipse,以保持代码风格的一致性。 - 在`Window &gt; Preferences &gt; Java &gt; Code Style &gt; Formatter`中导入`android-formatting...

    用 Eclipse 开发Android 程序配置法

    为了保持代码风格的一致性,导入`android-formatting.xml`和`android.importorder`文件。前者定义了代码格式化规则,后者规定了import语句的排序。在Eclipse中,可以通过以下路径导入这两份文件: 1. `window -&gt; ...

    Eclipse开发Android源码

    - **导入格式化和导入顺序配置**:将`android-formatting.xml`和`.importorder`文件导入Eclipse,分别用于设置代码风格和导入顺序。在Eclipse的偏好设置中,通过`Window -&gt; Preferences -&gt; Java -&gt; Code Style`进行...

    android eclipse code style

    eclipse开发android,代码标准...1、Preference-&gt;Java-&gt;Code Style-&gt;Formatter-&gt;Import-&gt;添加android-formatting.xml文件 2、Preference-&gt;Java-&gt;Code Style-&gt;Organize Imports-&gt;Import-&gt;添加android.importorder文件

    eclipse代码规范myCodeFormatter

    在开发Android应用时,遵循统一的代码规范是非常重要的,它能提高代码的...通过导入Google提供的`android.importorder`和`android-formatting.xml`,可以在Eclipse环境中轻松实现这些规范,使得代码更加整洁、专业。

    eclipse中开发调试android源项目

    - **导入配置文件**:将`android-formatting.xml`和`android.importorder`文件导入到Eclipse中。 - **配置代码风格**:通过`Window -&gt; Preferences -&gt; Java -&gt; Code Style -&gt; Formatter`菜单导入`android-formatting...

    向Eclipse中导入Andriod方法

    - 将`development/ide/eclipse`目录下的`android-formatting.xml`和`android.importorder`文件导入Eclipse,分别用于设定代码格式和import顺序。 4. **安装插件**(可选): - 可以安装AnyEdit Tools等增强...

    Android入门之使用eclipse进行源码开发的方法

    1. 源码下载后,找到位于/home/mark/android_src/development/ide/eclipse的目录,这里有几个关键文件,如.android-classpath、.android-importorder和.android-formatting.xml,它们影响Eclipse的代码格式化和导入...

    The XML Files_ Using XML for Business-to-Business and Business-to-Consumer Application

    - **样式与转码**:XSLT(Extensible Stylesheet Language Transformations)和XSL-FO(XSL Formatting Objects)等技术用于将XML数据转换成其他格式,如HTML或PDF。 - **查询语言**:XPath和XQuery等语言为处理XML...

    android_source讲解

    1. **准备文件**:首先需要将`development/ide/eclipse/`目录下的`.classpath`文件复制到源代码的根目录下,并导入`android-formatting.xml`和`android.importorder`到Eclipse中。 - `.classpath`:定义了项目的类...

    在Eclipse中配置Android的import和format模板

    另外,为了使整个团队使用统一的格式化规则,可以将`android.formatting.xml`文件分发给团队成员。这个文件包含了你刚才配置的所有格式化规则。团队成员只需将此文件复制到他们的Eclipse安装目录下的`plugins/...

    Manning - Eclipse In Action.pdf

    - **Editing and Formatting**: Techniques for editing and formatting source code efficiently using Eclipse's built-in tools. - **Code Navigation**: Features that help navigate through large codebases, ...

    Eclipse插件\快捷键

    Eclipse Tidy - Cleanup HTML/XML Files - **Eclipse Tidy**:清理 HTML/XML 文件的工具。 **下载地址**:[http://eclipsetidy.sourceforge.net/](http://eclipsetidy.sourceforge.net/) #### 20. Checkclipse - ...

    android开发环境搭建

    首先,将MTK源码目录下的'alps\development\ide\eclipse'中的.classpath、android.importorder和android-formatting.xml文件复制到alps根目录。这些文件将帮助Eclipse正确地识别和格式化Android项目。接着,在...

    Android Studio实现格式化XML代码顺序

    之前用Eclipse时,格式化XML代码,也会把顺序格式化,这样比较方便,看起来也清晰明了。 比如: 用Eclipse格式化时,会变成: Android Studio解决办法: File &gt; Settings &gt; Code Style &gt; XML &gt; Set from &gt; ...

Global site tag (gtag.js) - Google Analytics