XML attributes order in Android Studio

前端 未结 6 1683
小蘑菇
小蘑菇 2020-12-12 11: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_* at

6条回答
  •  攒了一身酷
    2020-12-12 12:00

    UPDATE: the method I described below is an official way to fix this known issue, see: https://developer.android.com/studio/releases

    After updating to the newest Android Studio v3.4.2 I found that it doesn't format XML code properly anymore. For instance, attributes ordering didn't work in layouts. It did indentation and namespaces ordering only.

    I don't know the reason why it was broken after an update but was lucky to fix this:

    1. Go to Settings (CMD + , ) on Mac.
    2. Type format in Search box and click on Editor -> XML setting.
    3. Click on the Android tab and make the same settings here as it's on a picture:

    Settings on a picture quite normal, but of course you can tune this tab setting up to you.

    1. Go to the Arrangement tab and you will see something like this:

    The reason why attributes are not getting sorted - sorting rules are empty.

    1. To fix this:

      1. Choose Scheme and play with Default IDE and Project options. Switching between them may help.
      2. Or choose Scheme which suites your needs and then click on Set from... blue text in right upper corner -> Predefined Style -> Android. You will see restored rules in the window:

    I also chose Force rearrange - Always

    To save result – click Apply.

    Also you can click on a gear icon near Scheme and copy settings or even restore defaults.

    P.S. To me standard rules are comfortable, but it can be customized further. Here is an article which may help doing this: https://medium.com/@VeraKern/formatting-xml-layout-files-for-android-47aec62722fc

提交回复
热议问题