XML属性在Android Studio中是有序的

时间:2021-05-18 22:24:30

I'm facing problem with keeping proper order of XML attributes in Android Studio. As you can see below, the style attribute is between layout_* attributes, but I want it to be ordered by name (like in Eclipse). I'm using standard Intellij code formatter and the Android Studio gives users ability to set your own rules regarding XML ordering. The settings are located in Code Style -> XML -> Arrangement, but it seems not to work or I'm using it wrong. Any ideas how to order XML attributes by name using default code formatter?

我面临着在Android Studio中保持XML属性正确顺序的问题。正如您在下面看到的,style属性位于layout_*属性之间,但我希望它按名称(如Eclipse中)排序。我使用的是标准的Intellij代码格式化程序,Android Studio允许用户设置自己关于XML排序的规则。设置位于代码样式-> XML ->安排中,但似乎不工作,或者我使用错误。如何使用默认代码格式化程序对XML属性进行排序?

<TestView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    style="@style/BackgroundLight"
    android:layout_height="wrap_content">

</TestView>

3 个解决方案

#1


103  

Finally got it.

终于明白了。

  1. Choose File > Settings > Code Style > XML > Set from > Predefined Style > Android
  2. 从>预定义样式> Android中选择>设置>代码样式> XML >集
  3. Set File > Settings > Editor > Formatting > Show "Reformat Code" dialog
  4. 设置文件>设置>编辑>格式>显示“重新格式化代码”对话框。
  5. Run formatting on a XML file (CTRL+ALT+L by default)
  6. 在XML文件上运行格式(默认情况下是CTRL+ALT+L)
  7. You will see a popup window, set the Rearrange entries flag
  8. 您将看到一个弹出窗口,设置重排条目标志
  9. Disable Reformat Code dialog in the settings
  10. 在设置中禁用重新格式化代码对话框

This way every XML file formatting will set the attributes in a proper order.

这样,每个XML文件格式都会以适当的顺序设置属性。

Edit: Starting with Android Studio 0.2.6 release the XML Android style formatting is set by default, but you still have to set the Rearrange Entries flag manually.

编辑:从Android Studio 0.2.6发布开始,默认设置了XML Android样式的格式,但是仍然需要手动设置重新排列条目标志。

http://tools.android.com/recent/androidstudio026released

http://tools.android.com/recent/androidstudio026released

Automatically apply the Android XML code style if a code style hasn't already been customized. This will make it possible to automatically order XML attributes (check the "Rearrange Entries" checkbox in the "Code > Reformat Code..." dialog.)

如果代码样式尚未定制,则自动应用Android XML代码样式。这将使自动订购XML属性成为可能(在“代码>重新格式化代码…”对话框中选中“重新排列条目”复选框)。

#2


3  

To delete empty lines between them:

删除它们之间的空行:

Preferences > Code Style > XML > Other > Keep blank lines: 0

>代码样式> XML >其他>保持空行:0。

And then just reformat your XML files.

然后重新格式化你的XML文件。

#3


0  

There is a plugin for IntelliJ which might do what you're after...

有一个IntelliJ的插件可以做你想做的事情……

http://plugins.jetbrains.com/plugin/?idea&pluginId=6546

http://plugins.jetbrains.com/plugin/?idea&pluginId=6546

#1


103  

Finally got it.

终于明白了。

  1. Choose File > Settings > Code Style > XML > Set from > Predefined Style > Android
  2. 从>预定义样式> Android中选择>设置>代码样式> XML >集
  3. Set File > Settings > Editor > Formatting > Show "Reformat Code" dialog
  4. 设置文件>设置>编辑>格式>显示“重新格式化代码”对话框。
  5. Run formatting on a XML file (CTRL+ALT+L by default)
  6. 在XML文件上运行格式(默认情况下是CTRL+ALT+L)
  7. You will see a popup window, set the Rearrange entries flag
  8. 您将看到一个弹出窗口,设置重排条目标志
  9. Disable Reformat Code dialog in the settings
  10. 在设置中禁用重新格式化代码对话框

This way every XML file formatting will set the attributes in a proper order.

这样,每个XML文件格式都会以适当的顺序设置属性。

Edit: Starting with Android Studio 0.2.6 release the XML Android style formatting is set by default, but you still have to set the Rearrange Entries flag manually.

编辑:从Android Studio 0.2.6发布开始,默认设置了XML Android样式的格式,但是仍然需要手动设置重新排列条目标志。

http://tools.android.com/recent/androidstudio026released

http://tools.android.com/recent/androidstudio026released

Automatically apply the Android XML code style if a code style hasn't already been customized. This will make it possible to automatically order XML attributes (check the "Rearrange Entries" checkbox in the "Code > Reformat Code..." dialog.)

如果代码样式尚未定制,则自动应用Android XML代码样式。这将使自动订购XML属性成为可能(在“代码>重新格式化代码…”对话框中选中“重新排列条目”复选框)。

#2


3  

To delete empty lines between them:

删除它们之间的空行:

Preferences > Code Style > XML > Other > Keep blank lines: 0

>代码样式> XML >其他>保持空行:0。

And then just reformat your XML files.

然后重新格式化你的XML文件。

#3


0  

There is a plugin for IntelliJ which might do what you're after...

有一个IntelliJ的插件可以做你想做的事情……

http://plugins.jetbrains.com/plugin/?idea&pluginId=6546

http://plugins.jetbrains.com/plugin/?idea&pluginId=6546