当我尝试在Eclipse Helios中格式化代码时,通过按 ctrl + shift + f ,代码将被格式化,但是结果非常难看。例如,一行中有两个或三个语句。压痕也很差。
例如:格式化代码后,它看起来像:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/scannerbg">
<include android:id="@+id/headerLayout"
android:layout_alignParentTop="true" layout="@layout/headerlayout" />
<ListView android:layout_below="@id/headerLayout"
android:layout_height="fill_parent" android:layout_marginTop="5dp"
android:listSelector="@android:color/transparent" android:id="@+id/listView"
android:layout_width="fill_parent">
</ListView>
</RelativeLayout>
正如您在ListView
中所看到的,单行中有两到三个语句。
有人可以为此提供解决方案吗?
答案 0 :(得分:2)
在Eclipse中,从 Window 菜单中选择 Preferences 。展开 Android 节点,然后选择 Editors 子节点。在右侧窗格中,确保选中“使用标准Android XML样式格式化XML文件.. ”(第一个选项),以及其他选项。
Ctrl + Shift + f 和/或 Ctrl + i 应该按预期工作并给出这些结果:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/scannerbg"
android:orientation="vertical" >
<include
android:id="@+id/headerLayout"
android:layout_alignParentTop="true"
layout="@layout/headerlayout" />
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/headerLayout"
android:layout_marginTop="5dp"
android:listSelector="@android:color/transparent" >
</ListView>
</RelativeLayout>
答案 1 :(得分:1)
使用ADT版本20进行完美格式化。