简体   繁体   中英

How to configure xml code style in Android Studio?

I wanted to define few formatting styles for the XML in Android Studio. I found it under Settings-Editor-Code Style-XML. I thought that Arrangements tab could help me. But I have no idea how to put them to work. I searched if there were docs but couldn't find nor any questions asked about it anywhere.

For example here is an unformatted XML --->

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
             <stroke android:width="2dp" android:color="@color/colorPrimary" />
             <corners android:radius="4dp" />
             <solid android:color="@color/colorAccent"></solid>
             <gradient android:startColor="@color/colorPrimary" android:endColor="@color/colorAccent" android:angle="45"/>
        </shape>
    </item>
</selector>

while I want the formatter to format something like this, when I press 'Ctrl+Alt+L' --->

<?xml version="1.0" encoding="utf-8"?>

<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    >


    <item>            
        <shape>

            <stroke
                android:width="2dp"
                android:color="@color/colorPrimary"
                />
            <corners
                android:radius="4dp"
                />
            <solid
                android:color="@color/colorAccent"
                >
            </solid>
            <gradient
                android:angle="45"
                android:endColor="@color/colorAccent"
                android:startColor="@color/colorPrimary"
                />

        </shape>
    </item>


</selector>

So, can you show me how to configure the Arrangements tab to achieve custom format? Or is this not the right way? Thank you.

TO UPDATE: Thank you for suggesting 'Ctrl+Alt+L', but I know that and my question is about customizing the code style of Xml formatter given under Arrangements tab as I mentioned above.

For your example with the <selector> in xml, it looks like the arrangement doesn't need to be changed, as the elements are already in the order you want them to be.

To change the formatting, go to Preferences --> Editor --> Code Style --> XML, and click on the "Android" tab. Change the "Wrap attributes" value (under the "Value Resource Files and Selectors" heading) from the default of "Do not wrap", to "Wrap always". Click OK.

适用于Android的Android Studio XML首选项

Now, if you do a reformat via CMD + ALT + L or CTRL +ALT + L on the selector xml, it will produce the following:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <stroke android:width="2dp"
                    android:color="@color/colorPrimary"/>
            <corners android:radius="4dp"/>
            <solid android:color="@color/colorAccent"></solid>
            <gradient android:angle="45"
                      android:endColor="@color/colorAccent"
                      android:startColor="@color/colorPrimary"/>
        </shape>
    </item>
</selector>

It's not exactly as you desired, but at least the attributes have their own line, which aids readability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM