Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

code formatting - XML attributes order in Android Studio

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?

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

</TestView>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally got it.

  1. Choose File > Settings > Code Style > XML > Set from > Predefined Style > Android
  2. Set File > Settings > Editor > Formatting > Show "Reformat Code" dialog
  3. Run formatting on a XML file (CTRL+ALT+L by default)
  4. You will see a popup window, set the Rearrange entries flag
  5. Disable Reformat Code dialog in the settings

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

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.

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...