Ctrl + Shift + f(代码格式)在Eclipse Helios中无法正常工作

时间:2012-08-29 10:35:50

标签: keyboard-shortcuts code-formatting eclipse-3.6

当我尝试在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中所看到的,单行中有两到三个语句。

有人可以为此提供解决方案吗?

2 个答案:

答案 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进行完美格式化。